How to assign values to a multidimensional array?

14 次查看(过去 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!

采纳的回答

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);

更多回答(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