Borrar filtros
Borrar filtros

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

2 visualizaciones (últimos 30 días)
Stephen
Stephen el 13 de Nov. de 2012
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

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 14 de Nov. de 2012
Editada: Andrei Bobrov el 14 de Nov. de 2012
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 comentario
Stephen
Stephen el 14 de Nov. de 2012
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!

Iniciar sesión para comentar.

Más respuestas (1)

Matt Fig
Matt Fig el 13 de Nov. de 2012
Editada: Matt Fig el 13 de Nov. de 2012
(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 comentarios
Matt Fig
Matt Fig el 13 de Nov. de 2012
Stephen comments:
Bare with me a second and I'll quickly make a mock code :)
Stephen
Stephen el 13 de Nov. de 2012
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

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by