Info
この質問は閉じられています。 編集または回答するには再度開いてください。
Can I have a code that detects # of element transitions and return a matrix that has only rows of minimum element transitions?
1 回表示 (過去 30 日間)
古いコメントを表示
I have A=[1 0 0 2;1 0 2 2;2 0 1 2;0 0 0 2];
I want a code that eliminates the rows that have multiple element changes compared to other rows:
so in A, first row and second row has only one element change 3rd element ((I call it element transition)) so the code will return a new matrix B that has only the first two rows;
B=[1 0 0 2;1 0 2 2]
0 件のコメント
回答 (2 件)
James Tursa
2016 年 9 月 14 日
編集済み: James Tursa
2016 年 9 月 14 日
Assuming you compare a row to the previous row and allow at most one element to change, you can use the results of diff(A), count the number of elements that have changed, and discard all rows that have more than 1 change. E.g., (assuming A is not empty)
B = A([true;~(sum(logical(diff(A,1,1)),2)>1)],:);
1 件のコメント
Andrei Bobrov
2016 年 9 月 15 日
...row 2, 3 , 4 are compared to 1 then row 3, 4 compared to 2 and so on...
C = squeeze(sum(bsxfun(@minus,A,permute(A,[3,2,1]))~=0,2));
[ii,jj] = find(min(C(:)) == C);
B = arrayfun(@(x,y)A([x,y],:),jj,ii,'un',0);
0 件のコメント
この質問は閉じられています。
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!