# could anyone help me how to check the idx position containing value is not equal to zero

16 ビュー (過去 30 日間)
jaah navi 2019 年 6 月 5 日
コメント済み: jaah navi 2019 年 6 月 6 日
my code generates idx =[2 5 7 12 15]
Before i want to proceed i need to check whether the above mentioned position contain zero or non zero values.
If it contains non zero values i can proceed or else i need to change the idx position till i get all the position containing non zero values.

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

### 回答 (2 件)

Thorsten 2019 年 6 月 5 日
To check if any value of idx is zero, you can use
any(idx == 0)
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
jaah navi 2019 年 6 月 5 日
I am having matrix a=[4.1288 5.2574 0 4.9757 3.0069;
0 3.7672 4.7411 3.4989 0;
4.7132 0 4.0136 0 4.9751]
with respect to the matrix the idx= 2 6 8 12 15 gets generated in my code.
But i want to have idx in such a way that all idx should give the non zero position.
But in my code idx=2 6 12 points out zero.
So first i need to check idx points to zero if yes then i need to get another idx which contains only non zero values position.

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

Steven Lord 2019 年 6 月 5 日
a(idx) will give you the elements in a whose linear indices are stored in idx. You can check if they are exactly equal to 0 using the == operator.
a = [1 2 0 3 4; 0 5 6 7 0; 8 0 9 0 10];
idx = [2 6 8 12 15];
zeroLocations = a(idx) == 0;
indicesCorrespondingToZero = idx(zeroLocations)
If you need to modify idx, use zeroLocations to index into idx on assignment. Here I'm going to overwrite them with NaN but you could do computations on them, remove them, etc.
idx(zeroLocations) = NaN
##### 7 件のコメント5 件の古いコメントを表示5 件の古いコメントを非表示
Rik 2019 年 6 月 6 日
Since you don't care which element is chosen, you can use the second output of the max function.
a = [1 2 0 3 4; 0 5 6 7 0; 8 0 9 0 10];
%if a might contain negative values, use this instead:
%[val,row_idx]=max(abs(a),[],1);
[val,row_idx]=max(a,[],1);
if any(~nnz(val))
error('some cols have only zeros')
end
idx=sub2ind(size(a),row_idx,1:size(a,2));
jaah navi 2019 年 6 月 6 日
Thanks a lot.

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

### カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by