substituting a column into a 3D matrix

조회 수: 1 (최근 30일)
ily
ily 2011년 8월 20일
I have a matrix R = zeros(4,5,3) and I want to make the last column of each matrix a different number. such that given C is a vector
for n = 1: end R(:,end, n) = C(n)
I managed a solution where I have a column vector A which if I could substitute into R(:, end, :), but it says mismatch dimensions.
essentially, I want to substitute a column vector into the columns of a 3D matrix R without using a for loop.
  댓글 수: 1
Jan
Jan 2011년 8월 21일
"for n=1:end" is not valid. Better: "for n = 1:size(R, 3)".

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

채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 8월 20일
It's not clear when you mention "last column" for a 3-D matrix. Hope you mean one of the following two cases.
R=zeros(4,5,3);
A=1:20;
R(:,:,3)=reshape(A,size(R,1),size(R,2))
R=zeros(4,5,3);
A=1:12;
R(:,5,:)=reshape(A,size(R,1),size(R,3))
  댓글 수: 1
ily
ily 2011년 8월 22일
thanks. If we assumed the third dimension was a page. Then, I was trying to add a different vector into the last column of each page. I was trying to this without the for loop.
The second solution was very helpful. I understand the key is to make the vectors into a 2-D matrix.

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

추가 답변 (1개)

Jan
Jan 2011년 8월 21일
R = zeros(4,5,3);
A = [1, 2, 3];
R(:, end, :) = reshape(repmat(A, 4, 1), [4, 1, 3]);

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by