Dimensions of matrices being concatenated are not consistent

1 回表示 (過去 30 日間)
Linee Teh
Linee Teh 2020 年 4 月 10 日
コメント済み: Linee Teh 2020 年 4 月 12 日
t = 0:5640; % time taken for the system to run is from 0 to 5640s
i = 83*pi/180; % inclination angle in rad
w0 = 0.0011; % angular velocity in rad/s
a1 = sin(i)*cos(w0*t);
a2 = -cos(i);
a3 = 2*sin(i)*sin(w0*t);
A = [0 -a3 a2; a3 0 -a1; -a2 a1 0];
This is my coding and I would like to create a matrix A. It shows the error Error using vertcat: Dimensions of matrices being concatenated are not consistent when I computed it. I know the reason why, but how can I compute a matrix A with varying number of a1 and a3 while a2 is a constant?
  2 件のコメント
Peng Li
Peng Li 2020 年 4 月 10 日
I don’t really know the reason why you did this. But would an extension of a2 to size(t) fit your need? a2 = -cos(i) * ones(size(t));
Linee Teh
Linee Teh 2020 年 4 月 10 日
Thank you for your help and I successfully get a1, a2 and a3 with the same dimensions. However, my problem still haven't solve.
My full question is like below:
t = 0:5640; % time taken for the system to run is from 0 to 5640s
i = 83*pi/180; % inclination angle in rad
w0 = 0.0011; % angular velocity in rad/s
a1 = sin(i)*cos(w0*t);
a2 = -cos(i)*ones(size(t));
a3 = 2*sin(i)*sin(w0*t);
A = [1 0 0 0 a3 a2; 0 1 0 -a3 0 a1; 0 0 1 a2 -a1 0;...
-1 0 0 0 0 0; 0 -1 0 0 0 0; 0 0 -1 0 0 0;...
zeros(3) zeros(3)];
"Error using vertcat: Dimensions of matrices being concatenated are not consistent" still poping out with this code. I actually want to get A matrix with dimension 9 x 6 and also if possible, get matrix A for t = 0s, t=1s... t=5640s separately.
I hope my question is clear.

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

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 4 月 10 日
In MATLAB, rows of a matrix cannot have different number of elements. For that, you will need to use a cell array
t = 0:5640; % time taken for the system to run is from 0 to 5640s
i = 83*pi/180; % inclination angle in rad
w0 = 0.0011; % angular velocity in rad/s
a1 = sin(i)*cos(w0*t);
a2 = -cos(i);
a3 = 2*sin(i)*sin(w0*t);
A = {[0 -a3 a2]; [a3 0 -a1]; [-a2 a1 0]};
  3 件のコメント
Ameer Hamza
Ameer Hamza 2020 年 4 月 10 日
編集済み: Ameer Hamza 2020 年 4 月 10 日
Ok It is clear now. Try this
t = 0:5640; % time taken for the system to run is from 0 to 5640s
i = 83*pi/180; % inclination angle in rad
w0 = 0.0011; % angular velocity in rad/s
a1 = sin(i)*cos(w0*t);
a2 = -cos(i)*ones(size(t));
a3 = 2*sin(i)*sin(w0*t);
ONE = ones(1,1,numel(a1));
ZERO = zeros(1,1,numel(a1));
a1 = reshape(a1, 1,1,[]);
a2 = reshape(a2, 1,1,[]);
a3 = reshape(a3, 1,1,[]);
A = [ONE ZERO ZERO ZERO a3 a2;
ZERO ONE ZERO -a3 ZERO a1;
ZERO ZERO ONE a2 -a1 ZERO;
-ONE ZERO ZERO ZERO ZERO ZERO;
ZERO -ONE ZERO ZERO ZERO ZERO;
ZERO ZERO -ONE ZERO ZERO ZERO;
zeros(3,3,numel(a1)) zeros(3,3,numel(a1))];
The matrix A is 9x6x5641. You can access value like this
A(:,:,1) % <-- t = 0
A(:,:,2) % <-- t = 1
A(:,:,3) % <-- t = 2
..
..
A(:,:,5641) % <-- t = 5640
Linee Teh
Linee Teh 2020 年 4 月 12 日
Really thank you for your help!!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by