Replace zero in a matrix with value in previous row
12 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Mohit
el 1 de Feb. de 2014
Hi,
Can you please help me on how can I replace all zeroes in a matrix with the value in previous row?
e.g. if value in row 3 column 4 is 0, it should pick value in row 2 column 4.
I can do it using a for loop but I dont want to use that.
Thanks.
0 comentarios
Respuesta aceptada
Azzi Abdelmalek
el 1 de Feb. de 2014
Editada: Azzi Abdelmalek
el 1 de Feb. de 2014
A=[1 2 3 4;4 5 0 0;1 0 0 1 ;0 1 1 1]
while any(A(:)==0)
ii1=A==0;
ii2=circshift(ii1,[-1 0]);
A(ii1)=A(ii2);
end
4 comentarios
Jan
el 4 de Nov. de 2016
@Mido: Please open a new thread for a new question.
match = (A(:, 3)==0);
A(match, 3) = A(match, 2);
Más respuestas (5)
Shivaputra Narke
el 1 de Feb. de 2014
Now answer to your comment...
while(all(a(:))) a(find(a==0))=a(find(a==0)-1) end
3 comentarios
Captain Karnage
el 2 de Dic. de 2022
That expression doesn't work as written in a single line. Without a ; after the assignment, it gets an error 'Error: Illegal use of reserved keyword "end".` If I add the ; however, it doesn't error but it still doesn't work. I'm not sure why, because logically it seems like it should, but I just get the original a out when I run it.
DGM
el 3 de Dic. de 2022
Editada: DGM
el 3 de Dic. de 2022
The loop is never entered at all. You could make some modifications.
a = [0 5 9 13; 2 6 0 0; 3 0 0 15; 0 8 12 16]
na = numel(a);
while ~all(a(:)) % loop runs until there are no zeros
idx = find(a==0);
a(idx) = a(mod(idx-2,na)+1);
end
a
The redundant find() can be removed. Since this is based on decrementing the linear indices, this will fill zeros at the top of a column with content from the bottom of the prior column. Using mod() allows the wrapping behavior to extend across the ends of the array. Note that a(1,1) is filled from a(16,16).
Whether this wrapping behavior is intended or acceptable is a matter for the reader to decide.
Andrei Bobrov
el 1 de Feb. de 2014
l = A == 0;
ii = bsxfun(@plus,size(A,1)*(0:size(A,2)-1),cumsum(~l));
out = A;
out(l) = A(ii(l));
0 comentarios
Shivaputra Narke
el 1 de Feb. de 2014
May be this code can help...
% where a is your matrix a(find(a==0))=a(find(a==0)-1)
2 comentarios
Amit
el 1 de Feb. de 2014
Lets say your matrix is A
[m,n] = size(A);
An = A';
valx = find(~An); % This will give you zeros elements linear index
valx = valx(valx-n > 0);
An(valx) = An(valx-n);
A = An';
Ver también
Categorías
Más información sobre Logical 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!