multiplication of matrix with long data

Hi,
I have a long data (200K+ samples) from which a location vector L (3x1) and a matrix T (euler matrix 3x3) is generated and multiplied l = TL.
On the results of l (3x1) there are operations of sin and atan (to get the required results).
So far, I'm doing a for loop, which is not a Matlab way.
I'd like to speedup the performance of the code.
How can I do it?

4 Comments

What are you looping? The only part that you describe that involves the large data is the creation of L and T, and you do not describe the math involved in that.
Are you looping taking a subset of the data, doing the calculations, moving on to the next subset of data?
Thank you for taking the time to fill in that you are using R14SP2, that will help us not waste time suggesting some of the functions added recently.
The loop is on the matrix and vector generation. Each time, I'm taking only single sample of the data (i.e. x(i), y(i), z(i), phi(i),theta(i), psi(i))
PS, I'd be glad if you can also add some notes on the changes in latest releases.
"So far, I'm doing a for loop, which is not a Matlab way. "
Anything allowed by MATLAB IS MATLAB way.
Is there a specific reason you're still using release R14SP2 which is pretty close to twenty years old at this point?
There have been a lot of improvements in MATLAB in the past twenty years, which likely would improve performance of your code.

Sign in to comment.

 Accepted Answer

% Test data
N = 4;
Phi = 2*pi*rand(1,N);
Theta = 2*pi*rand(1,N);
Psi = 2*pi*rand(1,N);
X = rand(1,N);
Y = rand(1,N);
Z = rand(1,N);
Phi = reshape(Phi, 1, 1, []);
Theta = reshape(Theta, 1, 1, []);
Psi = reshape(Psi, 1, 1, []);
X = reshape(X, 1, 1, []);
Y = reshape(Y, 1, 1, []);
Z = reshape(Z, 1, 1, []);
sB = sin(Phi); cB = cos(Phi);
sD = sin(Theta); cD = cos(Theta);
sE = sin(Psi); cE = cos(Psi);
T = [ cD.*cE, -cB.*sE+cE.*sB.*sD, sB.*sE+cB.*sD.*cE;
cD.*sE, cB.*cE+sB.*sD.*sE, -cE.*sB+cB.*sD.*sE;
-sD, sB.*cD, cB.*cD ];
XYZ = [X; Y; Z];
xyz = pagemtimes(T, XYZ)
xyz =
xyz(:,:,1) = 0.3128 0.5127 -0.3960 xyz(:,:,2) = -0.7072 -0.7636 0.1150 xyz(:,:,3) = -0.1876 0.0750 1.0194 xyz(:,:,4) = 1.2058 -0.6237 0.2521
% For older MATLAB
%XYZ = [X, Y, Z];
%xyz = sum(T.*XYZ,2)

7 Comments

Please note that I'm working with R14 while the pagemtimes is from R2020b
Bruno Luong
Bruno Luong on 14 Jul 2022
Edited: Bruno Luong on 14 Jul 2022
You'll mtimesx (and few others) from File Exchange that work similarly for older MATLAB versions.
Or you can do this:
XYZ = [X, Y, Z];
xyz = sum(T.*XYZ,2)
@Bruno Luong, Additional Question - I neet to perform inverse of the T Matrix (the 3*3 part).
How this can be done?
For inverse of T instead of
xyz = pagemtimes(T, XYZ);
do this
xyz = pagemrdivide(T, XYZ);
Please note that I'm using old version Matlab R14
The inverse of T is its transpose T.', you need to form the inverse, noted here by iT
iT = [ cD.*cE, cD.*sE, -sD;
-cB.*sE+cE.*sB.*sD, cB.*cE+sB.*sD.*sE, sB.*cD;
sB.*sE+cB.*sD.*cE, cE.*sB+cB.*sD.*sE, cB.*cD ];
% For older MATLAB
XYZ = [X, Y, Z];
xyz = sum(iT.*XYZ,2)

Sign in to comment.

More Answers (0)

Products

Release

R14SP2

Asked:

on 14 Jul 2022

Commented:

on 28 Feb 2023

Community Treasure Hunt

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

Start Hunting!