Thread Subject: extracting 'important' elements from symmetric matrix

Subject: extracting 'important' elements from symmetric matrix

From: Juliette Salexa

Date: 9 Jul, 2009 19:37:01

Message: 1 of 8

Symmetric matrices have redundant information that I don't need.
I haven't found a matlab function that extracts only the 'important' elements and stores them into a separate array,
the best I could do was:

j=1;
for i =1:4
   while j<5
        g(i,j)=symm(i,j);
        j=j+1;
   end
   j=i+1;
end
h=g(find(g));

Is there an internal matlab function that can do this ??
and if not .. is there a better method than the algorithm above to do this ??

Subject: extracting 'important' elements from symmetric matrix

From: Matt Fig

Date: 9 Jul, 2009 19:50:16

Message: 2 of 8

You can do this with a few operations on triu(symm).

Subject: extracting 'important' elements from symmetric matrix

From: someone

Date: 9 Jul, 2009 19:53:01

Message: 3 of 8

"Juliette Salexa" <juliette.physicist@gmail.com> wrote in message <h35gst$rjl$1@fred.mathworks.com>...
> Symmetric matrices have redundant information that I don't need.
> I haven't found a matlab function that extracts only the 'important' elements and stores them into a separate array,
> the best I could do was:
>
> j=1;
> for i =1:4
> while j<5
> g(i,j)=symm(i,j);
> j=j+1;
> end
> j=i+1;
> end
> h=g(find(g));
>
> Is there an internal matlab function that can do this ??
> and if not .. is there a better method than the algorithm above to do this ??

doc sparse
doc diag

% and follow the other links at the bottom of the page.

Subject: extracting 'important' elements from symmetric matrix

From: Matt Fig

Date: 9 Jul, 2009 20:00:19

Message: 4 of 8

Or....


H = symm(triu(true(size(symm))))

Subject: extracting 'important' elements from symmetric matrix

From: Bruno Luong

Date: 9 Jul, 2009 20:19:01

Message: 5 of 8

Or using TRIU on FEX http://www.mathworks.com/matlabcentral/fileexchange/23391

symm(itriu(size(symm)))

Bruno

Subject: extracting 'important' elements from symmetric matrix

From: Matt Fig

Date: 9 Jul, 2009 20:33:02

Message: 6 of 8

Also, for future reference, your For loop could use massive improvement. It will fail if any element of symm is zero. (The find function gives this away.) A much better, and much faster, For loop would use pre-allocation and simple indexing. Here is an example:


L = length(A);
h = zeros(L*(L+1)/2,1); % Pre-allocate the result.
cnt = 1;
for ii = 1:L
   for jj = 1:ii
        h(cnt) = symm(ii,jj);
        cnt = cnt + 1; % This could be done away with too by using math on ii,jj.
   end
end

This is faster than:

H = symm(triu(true(size(symm))));

for small arrays, and more memory efficient.

Subject: extracting 'important' elements from symmetric matrix

From: Jos

Date: 9 Jul, 2009 21:09:02

Message: 7 of 8

"Juliette Salexa" <juliette.physicist@gmail.com> wrote in message <h35gst$rjl$1@fred.mathworks.com>...
> Symmetric matrices have redundant information that I don't need.
> I haven't found a matlab function that extracts only the 'important' elements and stores them into a separate array,
> the best I could do was:
>
> j=1;
> for i =1:4
> while j<5
> g(i,j)=symm(i,j);
> j=j+1;
> end
> j=i+1;
> end
> h=g(find(g));
>
> Is there an internal matlab function that can do this ??
> and if not .. is there a better method than the algorithm above to do this ??

Here is another approach:

N = size(symm,1) % == size(symm,2)!
idx = repmat(1:N,N,1) ;
h = symm(idx >= idx.')

Jos

Subject: extracting 'important' elements from symmetric matrix

From: Juliette Salexa

Date: 9 Jul, 2009 21:39:01

Message: 8 of 8

Thank you everyone.
symm(triu(true(size(symm)))) did the trick for me.

And yes, I always preallocate my matrices before they go into loops but I typed that just as a mindless example because I knew there would be a matlab way of doing it.

Thanks once again to everyone .. you've shown me some neat tricks!

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
triu Matt Fig 9 Jul, 2009 17:17:29
symm Sprinceana 9 Jul, 2009 16:55:28
reference Sprinceana 9 Jul, 2009 16:55:28
symmetric matrix Sprinceana 9 Jul, 2009 16:55:19
rssFeed for this Thread

Contact us at files@mathworks.com