Count the number of times a value repeats between certain rows in a matrix.

4 次查看(过去 30 天)
Hi all,....if i had a Matrix Ex...A(2,5,15,65,102
2,65,1,105,55
65,104,15,19,5).....so on.
how to find the total number of times a value repeats between Row1 and Row2....Ans 2,65
then Row2 and Row3....Ans 65.
then Row3 and 4......and so on..
Also inputting which rows i want to check.....maybe between row1 and row4 for example....then row2 and row5....and so on,would be very helpful
Thank you.
  2 个评论
Matt J
Matt J 2022-1-19
Why is ans 65 in rows 1 and 2? The number of common elements between the two rows is 2.
ray d
ray d 2022-1-19
ok....im looking to find the common elements between rows 1 and 2...which would be 2..(value 2 and 65).yes..
then row2 and 3.....answer would be 1...(value65).
that make better sense.....than the actual values.

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2022-1-19
A=[2,5,15,65,102;
2,65,1,105,55;
65,104,15,19,5];
skip=1;
A1=A(1:end-skip,:); A2=A(skip+1:end,:);
M=size(A,2);
result=0;
for i=0:M-1
result=result+sum(A1==circshift(A2,[0,i]),2);
end
result
result = 2×1
2 1
  3 个评论
Matt J
Matt J 2022-1-19
编辑:Matt J 2022-1-19
Note that if you have repetitions within a row,e.g.
A(1,:)=[2, 2 5,15,65,102]
you may get unexpected results.

请先登录,再进行评论。

更多回答(1 个)

David Hill
David Hill 2022-1-19
A=[2,5,15,65,102;2,65,1,105,55;65,104,15,19,5];
i=A(1,ismember(A(1,:),A(2,:)));
  2 个评论
ray d
ray d 2022-1-19
Hi Dave....thank you for the answer...yes,thats what im looking for...but ...to count down thro the rows.....count same elements in row1 and 2.....then row2 and 3....then row3 and 4....and so on.....done with a loop??....n and n+1 for the rows??....im unsure.

请先登录,再进行评论。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by