3D Matrix to replace a square matrix in a for loop?

2 次查看(过去 30 天)
Sorry if the question title isn't great; stuggled to come up with a good question to ask the following;
I've been using matrices to store values and quickly do the same equation with the different values stored in that matrix. This works fine for row/collumns for example;
t = [1, 2, 6, 16];
n = 5*t
Which produces; n = [5, 10, 30, 80]
Likewise I can also multiple two row (or collumn) matrices like;
a = [1, 3, 2, 2];
e = a.*n
Which produces; e = [5, 30, 60, 160]
That's worked perfectly for me, however I now have a 3 by 3 matrix which changes over time. At the moment I have it in a for loop such that;
for j = 1:P
Ba = [cos(K(j)),sin(K(j)),2;sin(K(j)),cos(K(j)),2;1,1,2];
XYZ(:,j) = B * ABC(:,j);
end
So the value of K changes over time, as does the collumn matrix ABC. So for each step of j there's a 3 by 3 matrix multiplied by a 3 by 1 matrix (which can be seen is actually a 3 by P matrix of which I'm drawing one collumn at a time) which should give a 3 by 1 matrix. These 3 by 1 matrices are put together to make a 3 by P matrix which I can draw from later.
My question is, is there any way to do this without the for loop? For example I know '3D matrices' exist in matlab, but I'm not sure if they'd be usable here to say store all the 3 by 3 Ba matrices in and then draw on them in turn without a for loop (much like I did with ABC being a 3 by P matrix which I drew from collum by collum).
If I've missed anything out just say and sorry if this is a really simple problem; I've tried searching for a solution but I always end up more confused than when I started!
Thanks in advance
P.S. Couldn't find it in the products dropdown, but my version of matlab is R2010a
UPDATE
Sorry, very crude but I think this acts as a suitable mockup;
example = zeros(3,5);
a = [pi, pi/2, 1, 2*pi, pi/3];
b = [1, 2, 3, 4, 5; 2, 4, 5, 7, 5; 2, 2, 4, 5, 7];
for j = 1:5
matrix_1 = [sin(a(j)),cos(a(j)),1;cos(a(j)),-sin(a(j)),0;1,-cos(a(j)),-sin(a(j))];
example(:,j) = matrix_1 * b(:,j);
end
example

采纳的回答

Andrei Bobrov
Andrei Bobrov 2012-11-14
编辑:Andrei Bobrov 2012-11-14
a = [pi, pi/2, 1, 2*pi, pi/3];
b = [1, 2, 3, 4, 5; 2, 4, 5, 7, 5; 2, 2, 4, 5, 7];
variant 1
s = sin(a);
c = cos(a);
n = numel(a);
ons = ones(1,n);
m1 = cat(3,[s;c;ons],[c;-s;-c],[ons;zeros(1,n);-s]);
example = sum(bsxfun(@times,m1,reshape(b.',1,n,[])),3);
variant 2
n = numel(a);
ons = ones(1,n);
s = sin(a);
c = cos(a);
m1 = [[s;c;ons],[c;-s;zeros(1,n)],[ons;-c;-s]];
example = reshape(sum(m1.*repmat(b,1,n)),size(b,1),[]);
  1 个评论
Stephen
Stephen 2012-11-14
Thank you very much! I was having problems with my code not working as expected and was using this to tidy things up and try and work out what was going wrong... turns out this fixed it completely! Took me a while to get the code you provided to work in my actual code but found what I had done wrong and it worked perfectly. Thank you very much!

请先登录,再进行评论。

更多回答(1 个)

Matt Fig
Matt Fig 2012-11-13
编辑:Matt Fig 2012-11-13
(To find your MATLAB version, type: ver)
Have you looked at BSXFUN? For better suggestions, give some data and a loop that actually run... Just a small example we can copy/paste that will be completely self sufficient and capture the salient details of the larger problem.
  2 个评论
Matt Fig
Matt Fig 2012-11-13
Stephen comments:
Bare with me a second and I'll quickly make a mock code :)
Stephen
Stephen 2012-11-13
Sorry for replying in the answer box and not editing my post, had a blank mind moment in my haste to reply, thanks for sorting that out for me

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by