本文介绍了合并两个数组Matlab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道执行此操作的最短路径,因此提出了一个问题,尽管可以通过多种方式完成.

I want to know the shortest route to do this, thus asking the question, though it can be done in many ways.

假设有两个数组

    A1 = [x1 y1
          x2 y2
          x3 y3
          0  0
          0  0
          0  0
          0  0
              ]

   A2 = [a1 b1
          a2 b2
          a3 b3
          a4 b4
          0  0
          0  0
          0  0
              ]

现在,如何以最短的方式合并A1和A2,这样

Now, how to merge A1 and A2 in the shortest way, such that

   A = [x1 y1
        x2 y2
        x3 y3
        a1 b1
        a2 b2
        a3 b3
        a4 b4]

在数组索引方面比较弱.我已经实现了两个for循环和一些if语句来执行此操作,但是我觉得这是最短的方法.你能帮忙吗?

Am weak when it comes to array indexing. I have implemented two for loops and some if statements to do this, but I feel there is a shortest way. Can you please help?

推荐答案

这将删除所有全零的行,并将A1放在A2的顶部.之所以可行,是因为max进行列最大值

This will remove any rows with all zeros, and put A1 on top of A2. This works because max does column-wise maxima

A=[A1(max(A1')>0,:);A2(max(A2')>0,:)]

这篇关于合并两个数组Matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 13:27