Fill a matrix with same values

3 visualizaciones (últimos 30 días)
Alessandro Masullo
Alessandro Masullo el 20 de Oct. de 2012
Hi! I have a matrix like this:
[ 1 8 9 7 7 4]
[ 0 0 0 0 0 0]
[ 0 0 0 0 0 0]
[ 9 8 6 5 5 1]
[ 0 0 0 0 0 0]
And I want to fill it like this
[ 1 8 9 7 7 4]
[ 1 8 9 7 7 4]
[ 1 8 9 7 7 4]
[ 9 8 6 5 5 1]
[ 9 8 6 5 5 1]
It seems to be very easy, but I cannot realize how to do it. Thank you in advice!

Respuesta aceptada

Matt J
Matt J el 20 de Oct. de 2012
Editada: Matt J el 20 de Oct. de 2012
Another way,
e=find(any(A,2));
[h,b]=histc(1:size(A,1),[e;inf]);
A=A(e(b),:);
  7 comentarios
Matt Fig
Matt Fig el 20 de Oct. de 2012
I knew what you were saying, IA. I agree completely with your main point - a point we have discussed before (but it does bear repeating!).
Cheers!
Azzi Abdelmalek
Azzi Abdelmalek el 20 de Oct. de 2012
+1 to Fig's comment-answer

Iniciar sesión para comentar.

Más respuestas (3)

Azzi Abdelmalek
Azzi Abdelmalek el 20 de Oct. de 2012
Editada: Azzi Abdelmalek el 20 de Oct. de 2012
A= [1 8 9 7 7 4
0 0 0 0 0 0
0 0 0 0 0 0
9 8 6 5 5 1
0 0 0 0 0 0]
for k=find(~all(A,2))'
A(k,:)=A(k-1,:)
end

Image Analyst
Image Analyst el 20 de Oct. de 2012
You're all making assumptions of criteria Alex did not give. Making no assumptions whatsoever, this is the fastest way I can think of:
A =[ 1 8 9 7 7 4
1 8 9 7 7 4
1 8 9 7 7 4
9 8 6 5 5 1
9 8 6 5 5 1]

Alessandro Masullo
Alessandro Masullo el 20 de Oct. de 2012
Thank you :D

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by