Multiplying 3X3 array by a 3x1
25 views (last 30 days)
Show older comments
Tommy Pestolesi
on 1 Jan 2020
Commented: Tommy Pestolesi
on 7 Jan 2020
Hello I am here trying to multiply contents of a 3x3 array by a 3x1 vector. The code I have developed is displayed below. The outputs I have for matricies C through H are what I am looking for but when I try to do some matrix math I get different Arrays with not quite the right outputs.
clear all;
close all;
% Import the data file
M = importdata('6x300_input_data.txt');
%u,v,w are inputs taken from the data file that are each their own column
u = M.data(:,3);
v = M.data(:,4);
w = M.data(:,5);
% Time variable in seconds that is pulled from data
t_in = M.data(:,6);
lamda = -60; %longitude in degrees
omega = 0.004; %Rate
phi = 30; %measurement in degrees
UVW = [u,v,w];
%Array to populate the serires of A matrices that will be multiplied by
%each instance of UVW
A = arrayfun(@(t_in)[ -sind(lamda - omega*t_in) cosd(lamda - omega*t_in) 0;
-sind(phi)*cosd(lamda-omega*t_in) -sind(phi)*sind(lamda - omega*t_in) cosd(phi);
cosd(phi)*cosd(lamda - omega*t_in) cosd(phi)*sind(lamda - omega*t_in) sind(phi)],t_in,'UniformOutput',false);
%proper outputs that are desired but want to find a better way...
C = A{1,1}*UVW(1,:)'
D = A{2,1}*UVW(2,:)'
E = A{3,1}*UVW(3,:)'
F = A{4,1}*UVW(4,:)'
G = A{5,1}*UVW(5,:)'
H = A{6,1}*UVW(6,:)'
disp(C:H);
0 Comments
Accepted Answer
Max Murphy
on 2 Jan 2020
You can probably improve on this and in terms of actual efficiency I am not sure it's better than what you have, but I am assuming you wanted something that makes it so you don't run a loop on all the matrix multiplies:
%%%% OLD %%%%
clear all;
close all;
%% Import the data file
M = importdata('6x300_input_data.txt');
%% u,v,w are inputs taken from the data file that are each their own column
u = M.data(:,3);
v = M.data(:,4);
w = M.data(:,5);
%% Time variable in seconds that is pulled from data
t_in = M.data(:,6);
lambda = -60; %longitude in degrees
omega = 0.004; %Rate
phi = 30; %measurement in degrees
UVW = [u,v,w];
%% Array to populate the series of A matrices that will be multiplied by
%each instance of UVW
A = arrayfun(@(t_in)[ -sind(lambda - omega*t_in) cosd(lambda - omega*t_in) 0;
-sind(phi)*cosd(lambda-omega*t_in) -sind(phi)*sind(lambda - omega*t_in) cosd(phi);
cosd(phi)*cosd(lambda - omega*t_in) cosd(phi)*sind(lambda - omega*t_in) sind(phi)],t_in,'UniformOutput',false);
%% proper outputs that are desired but want to find a better way...
C = A{1,1}*UVW(1,:)'
D = A{2,1}*UVW(2,:)'
E = A{3,1}*UVW(3,:)'
F = A{4,1}*UVW(4,:)'
G = A{5,1}*UVW(5,:)'
H = A{6,1}*UVW(6,:)'
disp(C:H);
%%%% NEW %%%%
%% General Idea:
a = A{2,1}(:);
b = repmat(UVW(2,:),3,1);
b = b(:);
p = a.*b; % individual products
%% Implement:
A_hat = [ -sind(lambda - omega*t_in), ...
cosd(lambda - omega*t_in), ...
zeros(size(t_in)), ...
-sind(phi)*cosd(lambda-omega*t_in), ...
-sind(phi)*sind(lambda - omega*t_in), ...
ones(size(t_in))*cosd(phi), ...
cosd(phi)*cosd(lambda - omega*t_in), ...
cosd(phi)*sind(lambda - omega*t_in), ...
ones(size(t_in))*sind(phi)];
B_hat = repmat(UVW,1,3);
P = A_hat .* B_hat;
%% I think differences may just be finite-math issues:
output = [sum(P(:,1:3),2), sum(P(:,4:6),2), sum(P(:,7:9),2)].';
disp('output(:,1) - C:');
disp(output(:,1) - C);
disp('output(:,2) - D:');
disp(output(:,2) - D);
disp('output(:,3) - E:');
disp(output(:,3) - E);
disp('output(:,4) - F:');
disp(output(:,4) - F);
disp('output(:,5) - G:');
disp(output(:,5) - G);
disp('output(:,6) - H:');
disp(output(:,6) - H);
More Answers (0)
See Also
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!