2×0 empty double matrix

조회 수: 9 (최근 30일)
sxh
sxh 2024년 9월 4일
댓글: Voss 2024년 9월 5일
trying to calculate a function with matrix arguments and matrix output: Question 1: is the following code the right way to do it? Question 2: why is SAB21 returning " 2×0 empty double matrix"?
A = magic(4)
A = 4x4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
S = anti_eye(4)
S = 4x4
0 0 0 1 0 0 1 0 0 1 0 0 1 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
S = star (S, A) %should return A
SAB11 = 2x2
11 5 2 16
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
SAB12 = 2x2
10 8 3 13
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
SAB21 = 2x0 empty double matrix
SAB22 = 2x2
6 12 15 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
S = 4x4
11 5 10 8 2 16 3 13 0 0 6 12 0 0 15 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
function SAB = star(SA,SB)
% Redheffer Star Product
SAB11 = SA (1:2,1:2,:) + SA (1:2,3:4,:) *((eye(2) - SB (1:2,1:2,:)* SA (3:4,3:4,:))^(-1)) *SB (1:2,1:2,:) *SA (3:4,1:2,:)
SAB12 = SA (1:2,3:4,:) *((eye(2) - SB (1:2,1:2,:) *SA (3:4,3:4,:))^(-1)) *SB(1:2,3:4,:)
SAB21 = SB (3:4,1:2,:) *((eye(2) - SA (3:4,3:4,:) *SB (1:2,1:2,:))^(-1)) *SA(3:4,2:1,:)
SAB22 = SB (3:4,3:4,:) + SB (3:4,1:2,:) *((eye(2) - SA (3:4,3:4,:) *SB (1:2,1:2,:))^(-1)) *SA (3:4,3:4,:) *SB (1:2,3:4,:)
SAB (1:2, 1:2 ,:) = SAB11;
SAB (1:2, 3:4 ,:) = SAB12;
SAB (3:4, 2:1, :) = SAB21;
SAB (3:4, 3:4, :) = SAB22;
end
function ans = anti_eye(n)
% anti Identity matrix
flipud(eye(n));
end

채택된 답변

Voss
Voss 2024년 9월 4일
편집: Voss 2024년 9월 4일
"why is SAB21 returning " 2×0 empty double matrix"?"
Because the expression 2:1 evaluates to an empty vector.
SAB21 = SB (3:4,1:2,:) *((eye(2) - SA (3:4,3:4,:) *SB (1:2,1:2,:))^(-1)) *SA(3:4,2:1,:)
% ^^^
2:1
ans = 1x0 empty double row vector
I guess you meant 2:-1:1, or simpler, [2 1]:
2:-1:1
ans = 1x2
2 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[2 1]
ans = 1x2
2 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Example:
M = magic(4)
M = 4x4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
M(3:4,2:1,:)
ans = 2x0 empty double matrix
M(3:4,[2 1],:)
ans = 2x2
7 9 14 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
And by the way, the colon indexing in the third dimension, e.g.:
SA(3:4,[2 1],:)
% ^^
is not necessary anywhere in the code because all the arrays are two-dimensional (i.e., matrices). That is, you can just write
SA(3:4,[2 1])
etc.
  댓글 수: 2
sxh
sxh 2024년 9월 5일
Sharp eyes Voss! Many thanks
Voss
Voss 2024년 9월 5일
You're welcome!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Colormaps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by