MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
Asked by pammy on 9 May 2013

i've a matrix named A of size 2 x 16421

i want to compare one row with the other rows and whenever there is a match it spaecifies their index i mean which row number matches

A=[ 12 45 32 56 19 89 12 45 1 0 0 0 44 21 32 56 32 56 12 45 33 78 90 32 19 89 and so on....]

it starts with 12 45 and compare it with all other rows and shows there index value next it starts with 32 56 and compare it with other rows and when there is a match shows their index.

when i traverse it one by one it takes very long time can anybody tell me how can i do so?

*No products are associated with this question.*

Answer by Image Analyst on 9 May 2013

Accepted answer

I would think you could use sortrows(). Be sure to consider both outputs of sortrows().

John Doe on 9 May 2013

@Image Analyst: Do you know if `sortrows()` will be faster (or otherwise better) than `find()` in this case?

Image Analyst on 10 May 2013

Honestly I don't see how your answer even does what Pammy wants. Your code finds rows where the first column equals the second column. That's not at all what she asked for - not sure why she accepted that answer.

Pammy, here's some code to get you part of the way there:

A=[ 12 45 32 56 19 89 12 45 1 0 0 0 44 21 32 56 32 56 12 45 33 78 90 32 19 89 ] [sortedRows, originalRowNumbers] = sortrows(A) count = 1; for k = 2 : size(sortedRows, 1) if sortedRows(k, 1) == sortedRows(k-1, 1) && ... sortedRows(k, 2) == sortedRows(k-1, 2) matchingRows{count} = {[originalRowNumbers(k), originalRowNumbers(k-1)]}; count = count + 1; end end matchingRows

## 0 Comments