matrix with vectors: multiplication and generation issue

2 ビュー (過去 30 日間)
sharay
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 件のコメント
Stephen23
Stephen23 2019 年 9 月 10 日
What size is the expected output?
sharay
sharay 2019 年 9 月 10 日
In principle, output=[ Y(j); 0] this 0 can be replaced by any element, wher j is again a vector.

サインインしてコメントする。

採用された回答

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)
  1 件のコメント
sharay
sharay 2019 年 10 月 4 日
thanks, it is working

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by