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

2 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by