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

1 visualización (últimos 30 días)
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

Respuestas (1)

Azzi Abdelmalek
Azzi Abdelmalek el 29 de Mzo. de 2016
Editada: Azzi Abdelmalek el 29 de Mzo. de 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)]

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by