how to find a pattern repetition in rows of a matrix
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Image Analyst
2018-2-28
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 String Parsing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!