How to assign values to a multidimensional array?
조회 수: 13 (최근 30일)
이전 댓글 표시
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!
댓글 수: 0
채택된 답변
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);
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!