Matlab sparse matrix does not come out as I would expect

조회 수: 5 (최근 30일)
Zeyuan
Zeyuan 2025년 2월 3일
댓글: Zeyuan 2025년 2월 3일
This is my code:
S=sparse([1 3 2 1 4],[3 1 4 1 4],[1 2 3 4 5],4,4)
full(s)
However, when I run it, I get:
'S =
(1,1) 4
(3,1) 2
(1,3) 1
(2,4) 3
(4,4) 5
ans=
0 0 0 1
0 1 0 0
1 2 0 0
0 0 3 0'
Please tell me why ans is not [4 0 1 0;0 0 0 3; 2 0 0 0; 0 0 0 5]?

채택된 답변

John D'Errico
John D'Errico 2025년 2월 3일
편집: John D'Errico 2025년 2월 3일
S=sparse([1 3 2 1 4],[3 1 4 1 4],[1 2 3 4 5],4,4)
S = 4x4 sparse double matrix (5 nonzeros)
(1,1) 4 (3,1) 2 (1,3) 1 (2,4) 3 (4,4) 5
full(S)
ans = 4×4
4 0 1 0 0 0 0 3 2 0 0 0 0 0 0 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
It works just fine.
What did you do wrong? LOOK CAREFULLY AT WHAT YOU WROTE. MATLAB is case sensitive in variable names.
You created the matrix S. Then you looked at full(s). Should MATLAB know that you wanted to see what was in the variable S, and not in the variable s? The mind reading toolbox never works correctly. ;-)

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by