how to find a pattern repetition in rows of a matrix
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Antworten (1)
Image Analyst
am 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().
0 Kommentare
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!