Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Circshift applied to columns by different amounts

Subject: Circshift applied to columns by different amounts

From: Tyler

Date: 16 Mar, 2012 22:33:12

Message: 1 of 6

Hi everyone:

I have a large matrix in the form:

a1 =
[1 1 1 1;
1 1 1 NaN;
1 1 NaN NaN;
1 NaN NaN NaN];

and I need to have it each column shift down by the number of NaNs in
that column yielding:

a1New =
[1 NaN NaN NaN;
1 1 NaN NaN;
1 1 1 NaN;
1 1 1 1];

Is there an efficient way to do this?

Cheers,

t.

Subject: Circshift applied to columns by different amounts

From: ImageAnalyst

Date: 16 Mar, 2012 23:07:31

Message: 2 of 6

On Mar 16, 6:33 pm, Tyler <hayes.ty...@gmail.com> wrote:
> Hi everyone:
>
> I have a large matrix in the form:
>
> a1 =
> [1 1 1 1;
> 1 1 1 NaN;
> 1 1 NaN NaN;
> 1 NaN NaN NaN];
>
> and I need to have it each column shift down by the number of NaNs in
> that column yielding:
>
> a1New =
> [1 NaN NaN NaN;
> 1 1 NaN NaN;
> 1 1 1 NaN;
> 1 1 1 1];
>
> Is there an efficient way to do this?
>
> Cheers,
>
> t.

------------------------------------------------
If they all look like that, with the bottoms of the columns being nans
and all the numbers being the same (1 in this case), you can use
flipud().

Subject: Circshift applied to columns by different amounts

From: Tyler

Date: 16 Mar, 2012 23:19:49

Message: 3 of 6

Right. Okay, bad example. In reality its more like

a1 =
[1 2 3 4;
5 5 6 7;
1 3 4 NaN;
15 2 NaN NaN;
9 NaN NaN NaN];

and I'll need:

a1new = [
1 NaN NaN NaN;
5 2 NaN NaN;
1 5 3 NaN;
15 3 6 4;
9 2 4 7];

Sorry about that.... :$

t.

Subject: Circshift applied to columns by different amounts

From: parag2489@gmail.com

Date: 17 Mar, 2012 00:32:52

Message: 4 of 6

I think this solves the query, not the most efficient, but works...

a1=[1 2 3 4; 5 5 6 7; 1 3 4 NaN; 15 2 NaN NaN; 9 NaN NaN NaN];
>> a1

a1 =

     1 2 3 4
     5 5 6 7
     1 3 4 NaN
    15 2 NaN NaN
     9 NaN NaN NaN

>> a2=isnan(a1);
for i=1:size(a1,2)
a3=a2(:,i);
a1new(:,i)=circshift(a1(:,i),length(a3(a3==1)));
end
a1new

a1new =

     1 NaN NaN NaN
     5 2 NaN NaN
     1 5 3 NaN
    15 3 6 4
     9 2 4 7

Subject: Circshift applied to columns by different amounts

From: Roger Stafford

Date: 17 Mar, 2012 03:43:11

Message: 5 of 6

Tyler <hayes.tyler@gmail.com> wrote in message <b4d6be8b-3520-4e7b-8d44-726651eddcd4@g2g2000vbs.googlegroups.com>...
> .......
> and I need to have it each column shift down by the number of NaNs in
> that column yielding:
> ......
- - - - - - - - - - -
 [m,n] = size(a1);
 [I,J] = ndgrid(0:m-1,0:n-1);
 a1new = a1(1+m*J+mod(bsxfun(@minus,I,sum(isnan(a1),1)),m));

Roger Stafford

Subject: Circshift applied to columns by different amounts

From: Tyler

Date: 17 Mar, 2012 03:53:28

Message: 6 of 6

Thanks guys!

Tags for this Thread

No tags are associated with this thread.

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.

Contact us