What is an alternative FIND function that find indices and values of both ZERO and NONZERO elements in Matlab ?
25 次查看(过去 30 天)
显示 更早的评论
Hello,
I am using Matlab and I would like to know how to find indices/values of both zero and nonzero elements of a given vector ? Can anyone help ?
For example:
X = [1 0 2; 0 1 1; 0 0 4];
How can I find indices of the elements in the first raw i.e. '1'; '0'; '2' ?
Thank you!
3 个评论
采纳的回答
Stephen23
2016-2-3
编辑:Stephen23
2016-2-3
>> E = [2, 0, 5, 9];
>> [R,C] = ind2sub(size(E),1:numel(E))
R =
1 1 1 1
C =
1 2 3 4
Or using your original example:
>> X = [1 0 2; 0 1 1; 0 0 4];
>> [R,C] = ind2sub(size(X),1:numel(X))
R =
1 2 3 1 2 3 1 2 3
C =
1 1 1 2 2 2 3 3 3
or just the first row (as your question request) is very simple:
>> C = 1:size(X,2)
C =
1 2 3
>> R = ones(size(C))
R =
1 1 1
更多回答(1 个)
Image Analyst
2016-2-3
Try this:
X = [1 0 2; 0 1 1; 0 0 4]
[zeroRows, zeroColumns] = find(X == 0)
[nonZeroRows, nonZeroColumns] = find(X ~= 0)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!