matrix with vectors: multiplication and generation issue

4 次查看(过去 30 天)
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?

采纳的回答

Jon
Jon 2019-9-10
编辑:Jon 2019-9-10
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 个评论
sharay
sharay 2019-10-18
编辑:sharay 2019-10-18
Hi yes, this is the case.Main problem I am facing is with ploting. for every element on x i have 16 values.
Jon
Jon 2019-10-18
编辑:Jon 2019-10-18
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.

请先登录,再进行评论。

更多回答(1 个)

Jon
Jon 2019-10-3
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)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by