How i fix the error "Matrix dimensions must agree?"
조회 수: 2 (최근 30일)
이전 댓글 표시
xi=randi([10,30],4,4)
A=xi(1,:)
B=xi(:,1)
C=A - B
Why i get matrix dimensions must agree error.
댓글 수: 0
답변 (2개)
Chunru
2021년 7월 23일
Which version of matlab you are using?
xi=randi([10,30],4,4)
A=xi(1,:)
B=xi(:,1)
% arrays with implicit expansion for newer matlab
% both A and B are expanded into compatible size before operation
C = A - B
Image Analyst
2021년 7월 23일
Not sure what you want or are expecting because you forgot to tell us. Perhaps you want a 1-dimensional, 4-element vector. If so, here is how to do it.
xi = randi([10, 30], 4, 4) % 4-by-4 matrix.
A = xi(1, :) % Row vector
B = xi(:, 1) % Column vector
C1 = A(:) - B(:) % C will be a column vector.
C2 = A - B' % C will be a column vector.
C1 =
0
-7
-16
2
C2 =
0 -7 -16 2
If you want a 2-D matrix, then C=A-B works with no error in recent versions of MATLAB that do implicit expansion. But you also forgot to list your release when you posted. Here is another chance to read the posting guidelines and edit your post to improve it:
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!