Thread Subject: returning a vector instead of a matrix !

Subject: returning a vector instead of a matrix !

From: julien

Date: 4 Jul, 2011 16:33:10

Message: 1 of 9

Hello mat-world !

My question might be trivial, I lost my afternoon trying to figure it out...(quite new in the mathworld)
I wanna perform operations on nonzero elements of a matrix.
basically :
A(A~=0) returns the nonzero elements of a matrix... but returns it as one vector.. I want matlab to do that column by column (or line by line).
and then doing operations like sum(A(A~=0) with the result coming column by column (or line by line).

i'm sure it's trivial, but I just cant find the solution :-/

any help ?

Subject: returning a vector instead of a matrix !

From: Matt J

Date: 4 Jul, 2011 16:49:09

Message: 2 of 9

"julien " <emo_neuro@hotmail.com> wrote in message <iusq06$ca7$1@newscl01ah.mathworks.com>...
> Hello mat-world !
>
> My question might be trivial, I lost my afternoon trying to figure it out...(quite new in the mathworld)
> I wanna perform operations on nonzero elements of a matrix.
> basically :
> A(A~=0) returns the nonzero elements of a matrix... but returns it as one vector.. I want matlab to do that column by column (or line by line).
> and then doing operations like sum(A(A~=0) with the result coming column by column (or line by line).
>
> i'm sure it's trivial, but I just cant find the solution :-/
=======

The best way really depends specifically on the operation you want to do. For summation, you would just do sum(A,1) or sum(A,2) because summation automatically ignores zeros.

To do a column-by-column MEAN operation (ignoring zeros) you would do as follows

result=sum(A,1)./sum(A~=0,1);

Subject: returning a vector instead of a matrix !

From: Matt J

Date: 4 Jul, 2011 17:02:10

Message: 3 of 9

"julien " <emo_neuro@hotmail.com> wrote in message <iusq06$ca7$1@newscl01ah.mathworks.com>...
> Hello mat-world !
>
> My question might be trivial, I lost my afternoon trying to figure it out...(quite new in the mathworld)
> I wanna perform operations on nonzero elements of a matrix.
> basically :
> A(A~=0) returns the nonzero elements of a matrix... but returns it as one vector.. I want matlab to do that column by column (or line by line).
> and then doing operations like sum(A(A~=0) with the result coming column by column (or line by line).
>
> i'm sure it's trivial, but I just cant find the solution :-/
>
> any help ?


If most of your matrix elements are zero, it might also be worthwhile to convert your matrix to a sparse matrix.

A=sparse(A);

When MATLAB does operations on sparse matrices, like column/row summing, it uses algorithms that explicitly ignore zeros.

Subject: returning a vector instead of a matrix !

From: julien

Date: 4 Jul, 2011 18:50:24

Message: 4 of 9

 
> If most of your matrix elements are zero, it might also be worthwhile to convert your matrix to a sparse matrix.
>
> A=sparse(A);
>
> When MATLAB does operations on sparse matrices, like column/row summing, it uses algorithms that explicitly ignore zeros.

First, thanks to both of you for replying.

My exemples of operations were badly chosen (as I seeked for a general solution if it exists)
I had the solution for simple sums or means, and tried the sparse trick too, but none was working on my case.

my problem more precisely :

I have a matrix A of complex (from a complex wavelet transform).
Let's say I have x points in my time series, for y different scales
I transpose and get : y columns * x lines.

Following some papers, I can locate significant coefficients and get a matrix B of the same size as A, filled with 0 (non-significant entry) and 1 (significant) entry.
If I do : A.*B = I get a matrix of coefficients, 0 + 0i meaning a non-significant/non-relevant entry.

What I'd like to do is to get for each column (i.e. scale or pseudo-frequency band), the circular mean of the time series.
0 is then a problem for angle calculations... I tried to replace zeros from the matrix A by NaN, but when I calculate the circular mean I only get NaN as a result.
Sparse trick doesn't work on this neither

I thought I could do :
ind = find(A~=0);
circ_mean(A(ind));

but it returns, once again, a global circ_mean, when I want a circular mean for each column.... that's my problem.

though I could get a circular mean by another way (summing angles and dividing by the number of significant coefficients of each column), the problem would come back if I want to go deeper and do a Rayleigh test for non-uniformity of circular data...

isn't there any general way to tell matlab : do what you do, but do it for every column instead ?

thanks in advance !

Subject: returning a vector instead of a matrix !

From: Matt J

Date: 4 Jul, 2011 19:26:10

Message: 5 of 9

"julien " <emo_neuro@hotmail.com> wrote in message <iut21g$1n2$1@newscl01ah.mathworks.com>...
>
> What I'd like to do is to get for each column (i.e. scale or pseudo-frequency band), the circular mean of the time series.
> 0 is then a problem for angle calculations... I tried to replace zeros from the matrix A by NaN, but when I calculate the circular mean I only get NaN as a result.
> Sparse trick doesn't work on this neither
=================
 
What's wrong with using a simple for-loop


 [i,j,s] = find(A);

result=nan(1,size(A,2)); %pre-allocate

for k=1:max(j) %loop over columns of A

 result(k) = circ_mean(s(j==k));

end

Subject: returning a vector instead of a matrix !

From: julien

Date: 4 Jul, 2011 21:26:10

Message: 6 of 9

"Matt J" wrote in message <iut44i$6p1$1@newscl01ah.mathworks.com>...
> "julien " <emo_neuro@hotmail.com> wrote in message <iut21g$1n2$1@newscl01ah.mathworks.com>...
> >
> > What I'd like to do is to get for each column (i.e. scale or pseudo-frequency band), the circular mean of the time series.
> > 0 is then a problem for angle calculations... I tried to replace zeros from the matrix A by NaN, but when I calculate the circular mean I only get NaN as a result.
> > Sparse trick doesn't work on this neither
> =================
>
> What's wrong with using a simple for-loop
>
>
> [i,j,s] = find(A);
>
> result=nan(1,size(A,2)); %pre-allocate
>
> for k=1:max(j) %loop over columns of A
>
> result(k) = circ_mean(s(j==k));
>
> end

////

what is wrong with simple for-loop is just me ;-) I'm a newbie, and though I try hard to find solutions through manuals, tutorials, I miss some things + lack of math/programming intuition and knowledge.

... but unfortunately your code above doesn't work for me :
it only outputs NaN in each column (right number of column though).

I thought it might be de circ_mean code's fault... but replacing it simply by "sum" gave the same output.

first of all, for that code you assume I replaced all zeros by NaN right ?
the code does output some thing when there is no NaN.. and do that right maths including the zeros in the calculation..

Subject: returning a vector instead of a matrix !

From: julien

Date: 4 Jul, 2011 21:28:09

Message: 7 of 9

"Matt J" wrote in message <iut44i$6p1$1@newscl01ah.mathworks.com>...
> "julien " <emo_neuro@hotmail.com> wrote in message <iut21g$1n2$1@newscl01ah.mathworks.com>...
> >
> > What I'd like to do is to get for each column (i.e. scale or pseudo-frequency band), the circular mean of the time series.
> > 0 is then a problem for angle calculations... I tried to replace zeros from the matrix A by NaN, but when I calculate the circular mean I only get NaN as a result.
> > Sparse trick doesn't work on this neither
> =================
>
> What's wrong with using a simple for-loop
>
>
> [i,j,s] = find(A);
>
> result=nan(1,size(A,2)); %pre-allocate
>
> for k=1:max(j) %loop over columns of A
>
> result(k) = circ_mean(s(j==k));
>
> end

////

what is wrong with simple for-loop is just me ;-) I'm a newbie, and though I try hard to find solutions through manuals, tutorials, I miss some things + lack of math/programming intuition and knowledge.

... but unfortunately your code above doesn't work for me :
it only outputs NaN in each column (right number of column though).

I thought it might be de circ_mean code's fault... but replacing it simply by "sum" gave the same output.

first of all, for that code you assume I replaced all zeros by NaN right ?
the code does output some thing when there is no NaN.. and do that right maths including the zeros in the calculation..

Subject: returning a vector instead of a matrix !

From: Matt J

Date: 5 Jul, 2011 00:59:11

Message: 8 of 9

"julien " <emo_neuro@hotmail.com> wrote in message <iutb99$nai$1@newscl01ah.mathworks.com>...
>
> first of all, for that code you assume I replaced all zeros by NaN right ?
>=================

I assumed that the matrix A contains no NaNs, only finite values. I also assume that if if some column A(:,k) contains onlly zeros, the code should return NaN for result(k). Regardless, here is a better implementation

[M,N]=size(A);
result=nan(1,N); %pre-allocate

for k=1:N %loop over columns of A

 tmp=A(:,k);
 tmp=tmp(~isnan(tmp) & tmp~=0);

 result(k) = circ_mean(tmp);

end
 

Subject: returning a vector instead of a matrix !

From: julien

Date: 5 Jul, 2011 09:33:07

Message: 9 of 9

"Matt J" wrote in message <iutnkv$mj3$1@newscl01ah.mathworks.com>...
> "julien " <emo_neuro@hotmail.com> wrote in message <iutb99$nai$1@newscl01ah.mathworks.com>...
> >
> > first of all, for that code you assume I replaced all zeros by NaN right ?
> >=================
>
> I assumed that the matrix A contains no NaNs, only finite values. I also assume that if if some column A(:,k) contains onlly zeros, the code should return NaN for result(k). Regardless, here is a better implementation
>
> [M,N]=size(A);
> result=nan(1,N); %pre-allocate
>
> for k=1:N %loop over columns of A
>
> tmp=A(:,k);
> tmp=tmp(~isnan(tmp) & tmp~=0);
>
> result(k) = circ_mean(tmp);
>
> end
> =======================

thank you Matt !!!!!!!! it works \o/ !!
well, it returns 0 instead of NaN when a column contains only 0, but I'm ok with it. I just have to check if there are only zeros or not.


a friend gave me that code :
for col=1:size(A,1)
    Atmp=A(col,:);
    s(col)=circ_mean(Atmp(Atmp~=0));
end

which worked for sum, mean, etc.. but not for circular statistics

your implementation works !! my greatest thanks !!
Let's compute now !!

you made my day, have a nice one !

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
sum column grea... julien 4 Jul, 2011 12:34:13
rssFeed for this Thread

Contact us at files@mathworks.com