matrix with vectors: multiplication and generation issue
4 views (last 30 days)
Show older comments
Hi I have confusion with the following problem.
Let there is matrix A=[ sin(i) 0; 0 cos(i)] where i is vector of 100 elements , B= [ a b; c d] and C=[e ; g].
Now I have to multiply B*A*C. Can someone help? I am confuse either I am suppose to use for loop or what?
2 Comments
Accepted Answer
Jon
on 10 Sep 2019
Edited: Jon
on 10 Sep 2019
Yes you could do this with a loop, for example, with just some arbitrary set values for i, a, b, c, d, e, g to illustrate
theta = linspace(0,2*pi,100);
a = 10;
b = 3;
c = 20;
d = 40;
e = 2;
g = 25;
% assign constant matrices
B = [a b;c d];
C = [e;g];
% preallocate array Z to hold result (one column for each value of theta)
numTheta = length(theta); % number of elements in theta
Z = zeros(2,numTheta)
for i = 1:numTheta
A = [sin(theta(i)) 0;0 cos(theta(i))];
Z(:,i) = B*A*C
end
10 Comments
Jon
on 18 Oct 2019
Edited: Jon
on 18 Oct 2019
If you can store your a values in a 1x100 matrix and B values in a matrix that is 100x4x4 then you can plot 16 curves using
Bplt = reshape(B,100,16);
plot(a,Bplt)
Note that Bplt is now a 100x16 matrix. Considering B to be made up of 100 4x4 matrices, each column of Bplt contains the 100 values for a given position in these 4x4 matrices. In particular the first column of Bplt are the 100 values for the 1,1 position in the collection of 4x4 matrices. The second column of Bplt are the 100 values for 2,1 position in the collection. Column 5 in Bplt corresponds to the 100 values for the 1,2 position in th collection etc.
More Answers (1)
Jon
on 3 Oct 2019
Z is a collection of 2d arrays, where the first index selects a particular 2d array. The tricky thing is that MATLAB considers a particular element of this collection, say Z(2,:,:) as a 1x2x2 array, not just a 2x2 array. To get rid of the extraneous first dimension you have to use the squeeze function. So for example if you want to multiply the 5th 2d array in Z by a 2x1 column vector [8;2] you can use
v = squeeze(Z(5,:,:))*[8;2]
If you are only interested in multiplying Z by the vector [1;0], this is equivalent to collecting together all of the first columns of each of the 2d arrays in Z. You can do this using
Y = Z(:,:,1)
See Also
Categories
Find more on Loops and Conditional Statements 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!