pasting row elements as matrix

5 visualizaciones (últimos 30 días)
nadirvirk Virk
nadirvirk Virk el 11 de Mayo de 2011
Hi All, I new with matlab and right now am into a difficulty such that,I have a matrix like below b11 =
1 2
3 4
5 6
7 8
9 10
11 12
13 14
Now I am trying to create a new matrix (C) from this b11 matrix such that all elements in first row are copied in C with N replications and then same should be done for the next row till we reach last rowof b11 and the order of the C matrix will be (N) x (cols(b11)*rows(b11))!
I am trying this two for loops such that
for p=1:2:11
for i=1:6
b111(:,p:p+1)=repmat(b11(i,:),174,1);
end
end
But instead of looping and replicating all elements of each row, I only get the last two rows replicated 6 times which is required to be last two columns of C matrixotherwise.
Any help will be great since I am just beginning here.
Thanks and Regards
Nader
  2 comentarios
Sean de Wolski
Sean de Wolski el 11 de Mayo de 2011
Can you provide the expected result for this example (or subset of this example)?
i.e.
What do you want it b11 = [1 2; 3 4]
Andrei Bobrov
Andrei Bobrov el 12 de Mayo de 2011
give the correct result for:
N = 2;
B11 = [1 2; 3 4];
C =?

Iniciar sesión para comentar.

Respuesta aceptada

Sean de Wolski
Sean de Wolski el 12 de Mayo de 2011
b = [ 1 2
3 4
5 6
7 8
9 10
11 12
13 14];
N = 7;
c = repmat(reshape(b.',numel(b),1).',N,1)
If this is not what you want, please give the result as Andrei and I have suggested.
  1 comentario
nadirvirk Virk
nadirvirk Virk el 12 de Mayo de 2011
thanks Sean de, this what I required. Great help!

Iniciar sesión para comentar.

Más respuestas (3)

Laura Proctor
Laura Proctor el 11 de Mayo de 2011
I think that you would like something like this:
N = 10;
C = repmat(b11,[1,1,N]);
C = permute(C,[3,2,1]); % to bring it to the size N * cols * rows
But, seeing the expected result would be helpful... I'm not sure if you want a 3D matrix or a 2D matrix - I created a 3D matrix because of what you term the "order of the C matrix".
Originally, upon reading your question, I thought something like this code would be the solution:
C = repmat(b1,1,N);

Sean de Wolski
Sean de Wolski el 11 de Mayo de 2011
Or do they mean:
C = kron(b1,ones(N,2))
?

nadirvirk Virk
nadirvirk Virk el 12 de Mayo de 2011
Thanks for your replies Laura and Sean de... My resultant matrix C or as i named in code as b111 is two dimensional matrix not three such that rows = N and columns K = rows(b11)*cols(b11)!
I will use the C as the resultant matrix name from here on for clarity Now repmat does not help me here because it tiles up the whole matrix b11 in N rows or columns!
But as I tried to explain my need is to get a resultant matrix C from the elements of b11. Which should take the first row across all the columns in b11 and copies it to C 'N' times in the first 2 columns (since cols of b11=2) then it repeats the same with the next row elements in b11 and paste them in 3rd and 4th column of C again N times and finishes off while copying the last row elements in C (again N times) in column K-1 and K (in this case they will be 13th and 14th)! Lets c if could be done somehow and I put the problem with more clarity! Thanks

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