Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

traversing a long matrix

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?

0 Comments

pammy

Products

No products are associated with this question.

2 Answers

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().

3 Comments

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
John Doe on 14 May 2013

I read the question wrong, and you are of course right, it doesn't answer the question. Well, I voted up yours, so it's at least on top...

Image Analyst
Answer by John Doe on 9 May 2013
Edited by John Doe on 9 May 2013
Accepted answer

I think this should work

ind = find(A(:,1) == A(:,2));

- Rob

0 Comments

John Doe

Contact us