Borrar filtros
Borrar filtros

how to convert 2*2 matrix into 256*256 matrix using for loop.

1 visualización (últimos 30 días)
glory pachnanda
glory pachnanda el 1 de Feb. de 2016
Editada: Stephen23 el 1 de Feb. de 2016
Suppose I have a matrix
b = [1 2 3
4 5 6
7 8 9]
and I want to extend this matrix like this
b = [ 1 2 3 1 2 3 1 2 3
4 5 6 4 5 6 4 5 6
7 8 9 7 8 9 7 8 9
1 2 3 1 2 3 1 2 3
4 5 6 4 5 6 4 5 6
7 8 9 7 8 9 7 8 9
1 2 3 1 2 3 1 2 3
4 5 6 4 5 6 4 5 6
7 8 9 7 8 9 7 8 9];
using for loop

Respuestas (1)

Stephen23
Stephen23 el 1 de Feb. de 2016
Editada: Stephen23 el 1 de Feb. de 2016
Just use repmat, it is much neater and faster than using a loop:
>> b = [1,2,3;4,5,6;7,8,9]
b =
1 2 3
4 5 6
7 8 9
>> R = 3;
>> C = 3;
>> repmat(b,R,C)
ans =
1 2 3 1 2 3 1 2 3
4 5 6 4 5 6 4 5 6
7 8 9 7 8 9 7 8 9
1 2 3 1 2 3 1 2 3
4 5 6 4 5 6 4 5 6
7 8 9 7 8 9 7 8 9
1 2 3 1 2 3 1 2 3
4 5 6 4 5 6 4 5 6
7 8 9 7 8 9 7 8 9
If you really want to use a slow loop, then try this:
S = size(b);
for r = (R-1)*S(1):-S(1):0
for c = (C-1)*S(2):-S(2):0
out(r+1:r+S(1),c+1:c+S(2)) = b;
end
end

Categorías

Más información sobre Creating and Concatenating Matrices 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