3D Matrix to replace a square matrix in a for loop?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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
0 Commenti
Risposta accettata
Andrei Bobrov
il 14 Nov 2012
Modificato: Andrei Bobrov
il 14 Nov 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),[]);
Più risposte (1)
Matt Fig
il 13 Nov 2012
Modificato: Matt Fig
il 13 Nov 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 Commenti
Vedere anche
Categorie
Scopri di più su Resizing and Reshaping Matrices in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!