How do I find the zero values in an array and place them into cells by using loops?

1 回表示 (過去 30 日間)
Glenn Martinez
Glenn Martinez 2016 年 3 月 29 日
編集済み: Glenn Martinez 2016 年 3 月 29 日
I am trying to create a function to locate the position and values of the zeroes in an array by using loops. When the zero values are located it would record the value, row location, and column location and place them into cells below it. I having trouble locating all of the zero values in the array. The code below only locates the last zero of the array:
function[p]=sparse_array_cell(A)
A =[1,0,2;0,0,1;3,0,2];
p = cell(2,3);
p{1,1} = 'Value';
p{1,2} = 'Row Location';
p{1,3} = 'Column Location';
for n = 1:size(A,2)
for i = 1:size(A,2)
if A(i,n) == 0;
t = i;
k = n;
p{2,1} = A(t,k);
p{2,2} = t;
p{2,3} = k;
end
end
end

回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 3 月 29 日
編集済み: Azzi Abdelmalek 2016 年 3 月 29 日
A =[1,0,2;0,0,1;3,0,2]
[ii,jj]=find(A==0)
v=[zeros(numel(ii),1) ii jj]
h={'value' 'row' 'column'}
out=[h; num2cell(v)]

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by