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 Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by