How to assign values to a multidimensional array?

조회 수: 13 (최근 30일)
EB
EB 2016년 1월 7일
댓글: EB 2016년 1월 7일
Is there a faster way how to assign values to every page of a multidimensional array, e.g. by using for loop. I have following problem:
deltaAlpha=0.5;
stiffness_element=zeros(4,4,10);
stiffness=[1459759416.70691 182469927.088364 -1459759416.70691 182469927.088364
182469927.088364 44529146.0388045 -182469927.088364 1088335.73328652
-1459759416.70691 -182469927.088364 1459759416.70691 -182469927.088364
182469927.088364 1088335.73328652 -182469927.088364 44529146.0388045];
I need to assign to every page of matrix stiffness_element the matrix stiffness, but at page 2 the columns and rows need to be multiplied by deltaAlpha. Is there a faster way how to do this instead of writting this:
stiffness_element(:,:,1)=stiffness;
stiffness_element(:,:,2)=deltaAlpha*stiffness;
stiffness_element(:,:,3)=stiffness;
stiffness_element(:,:,4)=stiffness;
stiffness_element(:,:,5)=stiffness;
stiffness_element(:,:,6)=stiffness;
stiffness_element(:,:,7)=stiffness;
stiffness_element(:,:,8)=stiffness;
stiffness_element(:,:,9)=stiffness;
stiffness_element(:,:,10)=stiffness;
I tryed to use for loop, just to assign values to every page of the big matrix, something like this:
for i = 1:length(stiffness_element)
stiffness_element(:,:,i) = stiffness;
end
but at the end I got a message: Assignment has fewer non-singleton rhs dimensions than non-singleton subscripts. Any suggestions how could I assign stiffness to stiffness_element and by taking into account multiplying the second page by deltaAlpha? thanks!

채택된 답변

Guillaume
Guillaume 2016년 1월 7일
Probably, the easiest way is to do:
stiffness_element = repmat(stiffness, [1 1 10]);
stiffness_element(:, :, 2) = deltaAlpha * stiffness_element(:, :, 2);
  댓글 수: 1
EB
EB 2016년 1월 7일
Thanks a lot! It is working, and it's so simple.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by