Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...