MATLAB Answers

matrix with vectors: multiplication and generation issue

9 ビュー (過去 30 日間)
sharay 2019 年 9 月 10 日
編集済み: Jon 2019 年 10 月 18 日
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 件のコメント

Stephen Cobeldick
Stephen Cobeldick 2019 年 9 月 10 日
What size is the expected output?
sharay 2019 年 9 月 10 日
In principle, output=[ Y(j); 0] this 0 can be replaced by any element, wher j is again a vector.

サインイン to comment.


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

  10 件のコメント

表示 7 件の古いコメント
Jon 2019 年 10 月 18 日
Sorry, it is still not clear what you are trying to do.
I see that you want to make a plot.
On this plot it seems that you would like the x axis values to be formed by the elements a vector a which has 100 elements.
Now in some way, for each of those 100 values you are going to plot something that is found in your matrix b which seems to be 4x4x100.
So there are 16 values 4*4 corresponding to each element of a. Do you want 16 curves?
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 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);
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.

サインイン to comment.

その他の回答 (1 件)

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)

  1 件のコメント

sharay 2019 年 10 月 4 日
thanks, it is working

サインイン to comment.

サインイン してこの質問に回答します。

Translated by