How to extract matrix element from an indexed matrix

1 次查看(过去 30 天)
Hi,
How can I extract matrix element from an indexed matrixS(:,:,k)? I know S(2,2) would extract 2nd row,2nd column but how to do it with S(:,:,k) which are calculated in loop k=1:n?
Thanks

回答(1 个)

Star Strider
Star Strider 2014-5-20
You can extract them as (MxN) matrices, but you have to do operations on them inside the loop (unless you want to do the extremely unwise and strongly discouraged operation of creating individually-named (MxN) matrices).
The squeeze function is your friend here.
For example, this works:
S = randi(25, 7, 6, 5);
v = randi(10, 6, 1);
for k1 = 1:size(S,3)
R(:,:) = squeeze(S(:,:,k1));
Q(:,k1) = R*v;
end
The Q matrix here contains the results of the vector multiplications inside the loop.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by