How do I create a 2D sparse matrix?

조회 수: 10 (최근 30일)
Marwa Helemish
Marwa Helemish 2011년 2월 8일
Hallo, When I want to creat a sparse matrix, and and according to the i verctor( index) the element must be equal =1
say i=[6 12 18 24 19 13 7 1]; A=sparse(i,i,ones(1,8)); then I have A =
(1,1) 1
(6,6) 1
(7,7) 1
(12,12) 1
(13,13) 1
(18,18) 1
(19,19) 1
(24,24) 1
Now:I want to creat a A 2D sparse matrix with vector i, but with every element are a 2D matrix 3x3 for exmaple A(1,1) is 2D matrix of dimension 3x3=[1 0 0;0 1 0; 0 0 1]; thank you in advance
  댓글 수: 1
Bruno Luong
Bruno Luong 2011년 2월 10일
A (sparse) matrix can contain numerical elements and nothing else. What exactly you want to do?

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

채택된 답변

Marwa Helemish
Marwa Helemish 2011년 2월 10일
sorry Andrew,i canot accept it because , it is not want I mean. Many thanks again
  댓글 수: 1
Andrew Newell
Andrew Newell 2011년 2월 10일
Then what do you mean? I suggest editing your original question to clarify it.

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

추가 답변 (2개)

Andrew Newell
Andrew Newell 2011년 2월 8일
Here is how you would do it for your example:
N = 6; % I assume this is a square matrix?
A = zeros(3,3,N^2);
A3 = eye(3);
indexList = [6 12 18 24 19 13 7 1];
for i=indexList
A(:,:,i) = A3;
end
A = num2cell(A,[1 2]);
A = reshape(A,N,N);
A = sparse(cell2mat(A));
EDIT: If you're really after a sparse cell array of matrices, try looking at Sparse cell arrays.
  댓글 수: 2
Todd Flanagan
Todd Flanagan 2011년 2월 8일
Marwa says, "thanks alot"
Todd Flanagan
Todd Flanagan 2011년 2월 8일
Andrew says, "If you like this answer, could you please accept it?"

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


James Tursa
James Tursa 2011년 3월 21일

카테고리

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