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 日間)
JacobM
JacobM 2016 年 9 月 14 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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]

回答 (2 件)

James Tursa
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 件のコメント
JacobM
JacobM 2016 年 9 月 15 日
Great!
Should we have a reference to compare to? I am thinking if MATLAB can find the minimum transitions among all, so it may be useful if I compare all rows to each other, i.e row 2, 3 , 4 are compared to 1 then row 3, 4 compared to 2 and so on. So by that I am certain that the returned row has minimum #transition of all elements compared to all rows, does this make sense? what do you think?

Andrei Bobrov
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);

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by