matrix with vectors: multiplication and generation issue

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 commentaires

What size is the expected output?
In principle, output=[ Y(j); 0] this 0 can be replaced by any element, wher j is again a vector.

Connectez-vous pour commenter.

 Réponse acceptée

Jon
Jon le 10 Sep 2019
Modifié(e) : Jon le 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 commentaires

Hi thank you v much. It is working now. But one more query. What if C is also 2*2 matrix.
Jon
Jon le 11 Sep 2019
Modifié(e) : Jon le 11 Sep 2019
If C is a 2 by 2 matrix then the result of B*A*C will also be 2 by 2. If you want to save those you could accumulate them in a 3 dimensional array, where the ith "page" is a 2 by 2 matrix corresponding to the ith value of theta. So you in the previous example you could change the lines
C = [e f;g h] % you of course will now need to assign f and h also
% preallocate
Z = zeros(numTheta,2,2) % 3 dimensional result array
and when you compute and store
Z(i,:,:) = B*A*C
Thank you v much . My problem is resolved now.
Hi, I am facing a problem. Now I got matrix Z but at the end I want to multiply Z with [1;0]. But as Z is 3D now, multiplication is not possible.
Hi, output of this matrix is 100*4*4 . so it mean i dont have 100 matrices of 4*4 dimensions. ? they are 100 rows and 4 coloums with 4 layers?
I'm not really sure what your question is. What matrix are you referring to that is 100 x4 x4? What do you want to do with it? What problem are you having?
In general a matrix that is 100x4x4 can be thought of either as a collection of 4, 100x4 matrices or as a collection of 100, 4x4 matrices. How you want to interpret just depends upon what is useful for your problem. In the end MATLAB is just storing 1600 values in a contiguous section of memory, and the array dimensions just help you index them in a convenient way for your problem.
In any case please clarify what you are trying to do and what your difficulty is and hopefully I or someone else can give you more help
hi,
thanks that i figured out before the reply. now i have one more problem.
to be clear let say i have matrix of a=1x100 dimension(one row and 100 columns) then b= 4x4 matrix (stack of 100 matrices). so I want to plot(a,b). Which i know not possible in simple way.
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
sharay le 18 Oct 2019
Modifié(e) : sharay le 18 Oct 2019
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 le 18 Oct 2019
Modifié(e) : Jon le 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.

Connectez-vous pour commenter.

Plus de réponses (1)

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)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by