Define matrix array as functions

1 次查看(过去 30 天)
I am a student, and looking for any kind help to correct my work bellow :
I define some variables as function of cos (t), and when I run this program, it works, no error information at the command window, and also they do have the same length.
But when I try to make a matrix 2x2 with those variables as it's components, it doesn't work. I would be grateful if somebody will kindly help me to fix it. Here my program I try to do.
Thank you.
  2 个评论
Torsten
Torsten 2022-8-20
编辑:Torsten 2022-8-20
  1. Please include your code as plain ascii, not as a graphics.
  2. Some variables in the code you posted are undefined. So even if we took the time to write down your code again, we cannot test it.
  3. What do you mean by But when I try to make a matrix 2x2 with those variables as it's components, it doesn't work. What is the error message you get ?
Sasha
Sasha 2022-8-20
编辑:Sasha 2022-8-20
Thank you for the kind reply.
The code that I posted earlier as an image is a part of the whole codes that I have made. For undefined code, I have replaced some parts as below.
The type of error information passed in the command window is
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side. Error in Fig3paper2017 (line 20)
this error refers to the last part (M1(n))
t1 =[0:0.01:89.99]; %angles in degree
Lt =length(t1);
sindt1 =sind(t1); %sin t1 in degree
cosdt1 =cosd(t1); %cos t1 in degree
sindt2 =0.5*sindt1; %sin t2 in degree
cosdt2 =sqrt(1-(sindt2.^2)); %cos t2 in degree
for n=1:Lt;
k1(n) =2*pi*cosdt1(n)/L; %variable 1 (k1)
k2(n) =2*pi*cosdt2(n)/L; %variable 2 (k2)
alp1(n) =k1(n)*(5.5*10^-7); %variable 3 (alpha1)
alp2(n) =k2(n)*(5.5*10^-7); %variable 4 (alpha2)
bet1(n) =k1(n)/k2(n); %variable 5 (beta1)
%Matrix components
M1a(n) =1+bet1(n)+alp1(n); %matrix component 1 (a)
M1b(n) =1-bet1(n)+alp1(n); %matrix component 2 (b)
M1c(n) =1-bet1(n)-alp1(n); %matrix component 3 (c)
M1d(n) =1+bet1(n)-alp1(n); %matrix component 4 (d)
%Matrix 1 (2 x 2)
M1(n) =[M1a(n) M1b(n);M1c(n) M1d(n)];
end

请先登录,再进行评论。

采纳的回答

Paul
Paul 2022-8-20
If you're trying to store M1 for all values of n, then M1 needs to be 2 x 2 x n
M1(:,:,n) = [M1a(n) M1b(n);M1c(n) M1d(n)];
Consider preallocation of all the varaibles being assigned to.
  4 个评论
Sasha
Sasha 2022-8-20
I want to use the matrix multiplication operator *
The problem is, matrix 1 has a size of 2x2xn (2x2 matrix for all values ​​of n), as well as matrix 2 .
Therefore, I will have matrix 3 as the result of M1(2x2xn)*M2(2x2xn). Here I need the component values ​​a b c d for all n values ​​of the 3 matrix.
If the sum of n is 1, then I just need to write
M3a=M3(1,1); %is the first component / a, of matrix 3
M3b=M3(1,2); %is the second component / b, of matrix 3
M3c=M3(2,1); %is the third component / c, of matrix 3
M4d=M3(1,1); %is the last component / d, of matrix 3
unfortunately, the number of n I have is thousands, therefore I don't know how to call the component values ​​of the matrix for all values ​​of n. It seems the code I made below is still wrong.
M3a(n)=M3(1,1)(n); %is the first component / a, of matrix 3, for all values of n
Paul
Paul 2022-8-21
If M1 and M2 are both 2 x 2 x n, and M3 is 2 x 2 x n, and each page of M3 should be the product of the corresponding pages of M1 and M2, then use pagemtimes
M3 = pagemtimes(M1,M2)
M3 is now 2 x 2 x n. The (1,1) elements of the pages of M3 is then
M3a = M3(1,1,:);
M3a will be 1 x 1 x n. Use squeeze to convert to column vector if desired
M3a = squeeze(M3a); % or M3a = squeeze(M3(1,1,:))

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by