Error using vertcat Dimensions of arrays being concatenated are not consistent.

1 回表示 (過去 30 日間)
Jerico Yabut
Jerico Yabut 2020 年 9 月 16 日
回答済み: VBBV 2021 年 12 月 9 日
matrix1 = input("matrix 1 is \n");
matrix2 = input("matrix 2 is \n");
[M, N] = size(matrix1);
[P, Q] = size(matrix2');
if N == P
X = matrix1*matrix2
disp(X)
else
fprintf("Matrix multiplication can't be done.")
end
This is my code and we are tasked to do simple multiplication of matrices. This works fine in 2x2 or 1x1 or even 16x16 matrices but when it comes to 1x3 or 3x4 or 5x7, it doesnt work anymore. Thank you for answering
  2 件のコメント
KSSV
KSSV 2020 年 9 月 16 日
What is c, d, I, J? Variables are not defined, how do you expect us to help you?
Jerico Yabut
Jerico Yabut 2020 年 9 月 16 日
Thanks for pointing that out. I already edited it.

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

回答 (2 件)

David Hill
David Hill 2020 年 9 月 16 日
matrix1 = input("matrix 1 is \n");
matrix2 = input("matrix 2 is \n");
[M, N] = size(matrix1);
[P, Q] = size(matrix2);
if N == P
X = matrix1*matrix2;
disp(X)
else
fprintf("Matrix multiplication can't be done.")
end
  1 件のコメント
Jerico Yabut
Jerico Yabut 2020 年 9 月 16 日
I tried this however it doesn't work at 1x3 or 4x1 or 5x7. It only works on let's just say 4x4 or 2x2 or 10x10.it says that "Error using vertcat Dimensions of arrays being concatenated are not consistent."

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


VBBV
VBBV 2021 年 12 月 9 日
matrix1 = rand(1,3)%input("matrix 1 is \n"); This is example of 1 x3 matrix
matrix1 = 1×3
0.7567 0.2314 0.3456
matrix2 = rand(3,3)%input("matrix 2 is \n");
matrix2 = 3×3
0.2531 0.3500 0.0379 0.0466 0.7056 0.3046 0.0829 0.0319 0.9818
[M, N] = size(matrix1);
[P, Q] = size(matrix2);
if N == P
disp('The product is ')
X = matrix1.*matrix2
else
fprintf("Matrix multiplication can't be done.")
end
The product is
X = 3×3
0.1915 0.0810 0.0131 0.0353 0.1633 0.1053 0.0627 0.0074 0.3393

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by