matlab: Sparse Function for any matrix

I am trying to write a sparse function code for any matrix size. For example if i have a matrix:
A = [ 0 0 0 5
0 2 0 0
1 3 0 0
0 0 4 0];
a=size(A);
b=size(A);
c=0;
position=0;
for i=1:a for j=1:b if A(i,j) ~=0
c=c+1;
position=position+1;
S(c,:)=[position,i,j,A(i,j)];
end
end
end
S
S --> is the storage matrix for all the non zero elements for A matrix. In addition to this information(Index,Row Number, Column Number, Value) how do I include two more columns in the matrix which shows the next element in row & the next element in column.

댓글 수: 6

James Tursa
James Tursa 2013년 10월 24일
Can you provide the expected result for the sample matrix you have shown? What do you want stored if there is no next non-zero element in the row or column?
Antony
Antony 2013년 10월 24일
The expected result is like a linked list representation of the matrix(Index, Value, first in row, First in col, next in row, next in col, row number and col number).
Antony
Antony 2013년 10월 24일
if there is no next non zero element in the row or column it should scan the rest of the rows and columns
Cedric
Cedric 2013년 10월 24일
Why don't you create just a display function which fits your needs, but still use MATLAB sparse matrices for computing?
James Tursa
James Tursa 2013년 10월 25일
@Antony: So you want a linked list that goes by rows, and a separate linked list that goes by columns? And both linked lists have wraparound to next row (or column) and even back to the beginning? Is that what you want?
Antony
Antony 2013년 10월 25일
@James: Yes.

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

답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 10월 24일

0 개 추천

Why you don't use sparse function?
B=sparse(A)

댓글 수: 1

Antony
Antony 2013년 10월 24일
The sparse function does not display all the results that I need. I am not worried about the size of the matrix or speed. I want to display more columns to the same sparse matrix according to my requirement.

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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2013년 10월 24일

댓글:

2013년 10월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by