필터 지우기
필터 지우기

Why am I getting a red message when trying to run this code.

조회 수: 2 (최근 30일)
Brandon
Brandon 2023년 11월 29일
댓글: Walter Roberson 2023년 11월 29일
I ran this code earlier, I can not find out why it is giving me an error.

채택된 답변

Dyuman Joshi
Dyuman Joshi 2023년 11월 29일
Based on the picture of the code you have attached, there seems to be a typo while defining x0.
%typo
% v
x0 = randn(10:1);
The most obvious correction is using a comma -
x0 = randn(10,1);
Update the code and check again.
If you still encounter an error, copy and paste your code and the full error message (i.e. all of the red text).
  댓글 수: 2
Dyuman Joshi
Dyuman Joshi 2023년 11월 29일
Reproducing the error -
A = rand(11,10);
x0 = randn(10:1);
ATA = A'*A;
ATA*x0
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each element of the matrix individually, use TIMES (.*) for elementwise multiplication.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2023년 11월 29일
In the future, please don't post a picture of your code, post your code itself (formatted using the first button of the Code section of the message editor's toolstrip.)
Your line 39 is incorrect. Compare what you wrote (with the semicolon removed, so you can see what it created):
x0 = randn(10:1)
x0 = []
with what you likely meant:
x0 = randn(10,1)
x0 = 10×1
0.0797 -0.1291 0.8241 0.5541 -0.4408 0.5157 0.5580 1.3633 -1.3299 0.3437
Using 10:1 attempts to create an array of that size, which results in you getting an empty array.
10:1
ans = 1×0 empty double row vector
If you'd flipped the two sizes it would "work" but you'd be creating a much larger array.
x1 = rand(1:10); % 10-dimensional!
x2 = rand(1,10);
whos x0 x1 x2
Name Size Bytes Class Attributes x0 10x1 80 double x1 10-D 29030400 double x2 1x10 80 double
For generality, I recommend you also avoid hard-coding sizes like 10. If you modified your A you'd also have to modify everywhere your code assumes A has 10 columns. If instead you computed the sizes of x0 and other intermediate arrays using the size of A your code is more flexible.
A = rand(5, 6);
numrows = size(A, 1) % or could use height(A)
numrows = 5
numcols = size(A, 2) % or could use width(A)
numcols = 6

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by