Why do I keep getting this error message? Error using ./ Matrix dimensions must agree.

1 回表示 (過去 30 日間)
Toritsereju Okotie
Toritsereju Okotie 2020 年 11 月 12 日
hello, I keep getting an error message about the matrix dimension. I think if i get the write sign to use for the division, the error will be fixed. Plese help me out if you can. thanks.
firstmatrix=[-2 -4.1 2 3; -2 1.1 2 3.3; 4.1 2 5 8.1; 1 3 2.2 1.5];
secondmatrix=[1 3 9 2; 5 8 4 3; 10 16 8 6; 2 3 1 8];
y=[-1; 2; -2; 4];
prompt='choose firstmatrix or secondmatrix';
A=input(prompt);
d=det(A);
if d~=0
eigenvalue=eig(A);
[V,D]=eig(A);
x=y./A;
else
disp('d')
disp('Calculation Complete')
end

回答 (2 件)

Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam 2020 年 11 月 12 日
if you want to solve the equations, use this:
x=A\y;
  9 件のコメント
Toritsereju Okotie
Toritsereju Okotie 2020 年 11 月 12 日
the answer to the input prompt is either the firstmatrix or secondmatrix
Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam 2020 年 11 月 12 日
use
disp(A)
to make sure that A is correct

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


Jon
Jon 2020 年 11 月 12 日
I can run your code without getting any error messages. Can you please be more specific about how to reproduce the error. Also what version of MATLAB are you running? Maybe an older one that does not handle implicit expansion?
Looking at your code though I wonder about what you are trying to do. In particular it seems like you never use the result of the eigenvalue calculation. You could compute x = y./A without using the results of the eigenvalue calculation. Also, that statement (x = y./A) seems strange, y is a length 4 vector and A is a 4 by 4 matrix. While MATLAB will handle this with explicit expansion, but I wonder if that is really what you want. In any case dimensioning errors would not be caused by the algebraic sign of one of the entries.
If you could try to further explain what your end objective is for this code people may be able to help you further.
  1 件のコメント
Toritsereju Okotie
Toritsereju Okotie 2020 年 11 月 12 日
I'm new to matlab so I'm just figuring thimgs out. The version I'm using is 2020. I'm trying tyo answer the question below
Write a script to do the following:
a) create the following matrices and vector y b) in a well presented way and with some attempt to guard against poor user input, ask the user to choose one of the matrices and assign it to a variable A.
c) if the determinant of A is nonzero, find the eigenvalues and eigenvectors of A and the solution x to the equation Ax=y, or display an appropriate message otherwise
d) clear the workspace and command window at the start of each run and output all results to the

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

カテゴリ

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