how to find a pattern repetition in rows of a matrix

1 view (last 30 days)
Hi,
I have a matrix similar to this:
A= [1 0 2 0;
2 1 0 1;
0 0 2 0;
2 1 1 1];
First I would like to select specific rows according to the last column of the matrix, for example, I would like to select the rows corresponding to the 0 value of the last column, in this case, are the first and third rows:
B=[ 1 0 2 ;
0 0 2 ]
Now I would like to see if in the rows of B a number in a certain position is repeated, in this case, 0 2 is the pattern repeated in the selected rows.
I obtained as output a matrix like this:
C=[2 3;
0 2]
where the first row contains the position of the elements repeated in B, and in the second row the corresponding value.
Until now I have this code: ind = find(A(:,end)==1); res = A(ind,1:end-1); for i=1:4 yes(i)=all(~diff(res(:,i))); end lab=find(yes); val=res(1,yes); out=[lab; val];
there is a more efficient way? thanks

Answers (1)

Image Analyst
Image Analyst on 28 Feb 2018
Is this homework? (Sounds like it)
Try this:
A= [1 0 2 0;
2 1 0 1;
0 0 2 0;
2 1 1 1];
rowsToExtract = A(:,end) == 0
B = A(rowsToExtract, 1:end-1)
For C, I'll give a hint of trying to use strfind() or ismember().

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!