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

조회 수: 1 (최근 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에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by