Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

Merge two arrays Matlab

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

Suppose there are two arrays

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

and

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

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]

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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,:)]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...