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

2 views (last 30 days)
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

Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 29 Mar 2016
Edited: Azzi Abdelmalek on 29 Mar 2016
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)]

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!