pasting row elements as matrix

조회 수: 1 (최근 30일)
nadirvirk Virk
nadirvirk Virk 2011년 5월 11일
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
Sean de Wolski
Sean de Wolski 2011년 5월 11일
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 2011년 5월 12일
give the correct result for:
N = 2;
B11 = [1 2; 3 4];
C =?

댓글을 달려면 로그인하십시오.

채택된 답변

Sean de Wolski
Sean de Wolski 2011년 5월 12일
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
nadirvirk Virk
nadirvirk Virk 2011년 5월 12일
thanks Sean de, this what I required. Great help!

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Laura Proctor
Laura Proctor 2011년 5월 11일
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 2011년 5월 11일
Or do they mean:
C = kron(b1,ones(N,2))
?

nadirvirk Virk
nadirvirk Virk 2011년 5월 12일
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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by