フィルターのクリア

minimum not zero in row

4 ビュー (過去 30 日間)
Marc Peña
Marc Peña 2018 年 5 月 10 日
編集済み: Jan 2018 年 5 月 10 日
i want to find a way to go from one row to the next one that's indicated by the position of the minimum number excluding 0s until i get to the row i wanted.
Dmat = zeros(7,7);
output = [0 0 0 0 0 0 0];
O=1;
i=O;
T=7;
u=T;
c=1;
output(1)=O;
Dmat(1,1:7)=[0 4 6 5 0 0 0];
Dmat(2,1:7)=[4 0 3 0 7 0 0];
Dmat(3,1:7)=[6 3 0 11 8 0 0];
Dmat(4,1:7)=[5 0 11 0 2 10 12];
Dmat(5,1:7)=[0 7 8 2 0 5 0];
Dmat(6,1:7)=[0 0 0 0 5 0 3];
Dmat(7,1:7)=[0 0 0 12 0 3 0];
while i~=u
val = min(Dmat(~ismember(Dmat(i,1:7),0)));
ind = find(val==Dmat(i,1:7));
Dmat(1:7,i)=0;
i=ind;
output(c+1)=i;
c=c+1;
end
I don't know why the function i¡~ismember is not working for the second iteration. thx.
  1 件のコメント
dpb
dpb 2018 年 5 月 10 日
What is the result intended to be? Isn't clear what "to go to the next one" really means.
However, to find the location of the min() in the row w/o counting zeros, something like
D=D(Dmat(i,:)~=0); % select the subset
imin=find(Dmat(i,:)==min(D),1); % find loc in original
Wrote with a temporary for clarity of operations...

サインインしてコメントする。

採用された回答

Jan
Jan 2018 年 5 月 10 日
編集済み: Jan 2018 年 5 月 10 日
The argument
Dmat(~ismember(Dmat(i,1:7),0))
considers the first column only. You want:
Dmat(c, ~ismember(Dmat(i,1:7),0))
or maybe
Dmat(~ismember(Dmat(i,1:7),0), c)
It is not getting clear to me, if you want to operate on rows or columns.
By the way: This is at least confusing:
O=1;
i=O;
T=7;
u=T;
c=1;
ismember is an overkill, if you want to exclude one value only. Easier:
val = min(Dmat(Dmat(i,1:7) ~= 0), c);
Instead of excluding the zeros dynamically, what about this:
Dmat = [0 4 6 5 0 0 0; ...
4 0 3 0 7 0 0; ...
6 3 0 11 8 0 0; ...
5 0 11 0 2 10 12; ...
0 7 8 2 0 5 0; ...
0 0 0 0 5 0 3; ...
0 0 0 12 0 3 0];
Dmat(Dmat == 0) = Inf;
Now you can search the minimum directly:
[~, ind] = min(Dmat(i, :))

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by