Got Questions? Get Answers.
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:
Cycling through a matrix

Subject: Cycling through a matrix

From: Justin

Date: 26 Oct, 2010 14:08:03

Message: 1 of 10

Hi,

I am trying to find a quick way to cycle through a matrix of size 72x4. However, I would only like to cycle through it every three rows, and not every single row. I have been attempting to get MATLAB to do this but in the for loop it only gives me the 1st three rows and does not continue until the end. So in the end I would like to see 24 3 row data matrices. Here is my code:

for j=1:length(x);
    ind=find(x(av_window));
    x=x(ind,:);

end;

where av_window=[1:3]' (number of rows) and x is the 72x4 matrix. There must be a simple way of defining a 3-row window , but it only works for the first three rows and not for the subsequent rows (i.e. 4,5,6 then 7,8,9 up until 72).

Please can someone suggest how to go about this problem.

Justin.

Subject: Cycling through a matrix

From: Sean

Date: 26 Oct, 2010 14:22:03

Message: 2 of 10

"Justin " <J.Krijnen@uea.ac.uk> wrote in message <ia6nc3$7ks$1@fred.mathworks.com>...
> Hi,
>
> I am trying to find a quick way to cycle through a matrix of size 72x4. However, I would only like to cycle through it every three rows, and not every single row. I have been attempting to get MATLAB to do this but in the for loop it only gives me the 1st three rows and does not continue until the end. So in the end I would like to see 24 3 row data matrices. Here is my code:
>
> for j=1:length(x);
> ind=find(x(av_window));
> x=x(ind,:);
>
> end;
>
> where av_window=[1:3]' (number of rows) and x is the 72x4 matrix. There must be a simple way of defining a 3-row window , but it only works for the first three rows and not for the subsequent rows (i.e. 4,5,6 then 7,8,9 up until 72).
>
> Please can someone suggest how to go about this problem.
>
> Justin.

On the first iteration x is set to be a 3x1 or less because you overwrite it:

I think what you want is something like this:
x = x(logical(x(:,1),:);

Or if you want to test the whole row and not just the first column
x = x(any(x,2),:);

Subject: Cycling through a matrix

From: Matt J

Date: 26 Oct, 2010 14:25:09

Message: 3 of 10

"Justin " <J.Krijnen@uea.ac.uk> wrote in message <ia6nc3$7ks$1@fred.mathworks.com>...


I doubt you want the line x=x(ind,:)
In the first pass of the loop, this will truncates x to at most 3 rows, making the rest of x impossible to iterate over. If you simply want to average groups of three rows together, do it as follows


tmp=mean(reshape(1:length(x),3,[]));
xAveraged=reshape(tmp,24,[]);

Subject: Cycling through a matrix

From: Rolf

Date: 26 Oct, 2010 14:27:04

Message: 4 of 10

"Justin " <J.Krijnen@uea.ac.uk> wrote in message <ia6nc3$7ks$1@fred.mathworks.com>...
> Hi,
>
> I am trying to find a quick way to cycle through a matrix of size 72x4. However, I would only like to cycle through it every three rows, and not every single row. I have been attempting to get MATLAB to do this but in the for loop it only gives me the 1st three rows and does not continue until the end. So in the end I would like to see 24 3 row data matrices. Here is my code:
>
> for j=1:length(x);
> ind=find(x(av_window));
> x=x(ind,:);
>
> end;
>
> where av_window=[1:3]' (number of rows) and x is the 72x4 matrix. There must be a simple way of defining a 3-row window , but it only works for the first three rows and not for the subsequent rows (i.e. 4,5,6 then 7,8,9 up until 72).
>
> Please can someone suggest how to go about this problem.
>
> Justin.
First of all, you shouldn't use find like that, find will now returns the non zero elements (their indexes) in the first three rows (see help)
Second, what you do then is reassign x to a part of the original x, which will leave you with only a part of x, the rest is gone.

But as an answer to your question you could use, for example:

for j=1:size(x,1)/3
    rows=x((3*i-2):3*n);
end

Subject: Cycling through a matrix

From: Rolf

Date: 26 Oct, 2010 14:31:03

Message: 5 of 10

"Rolf " <r.vermeer@student.removethis.utwente.nl> wrote in message <ia6ofo$org$1@fred.mathworks.com>...
> "Justin " <J.Krijnen@uea.ac.uk> wrote in message <ia6nc3$7ks$1@fred.mathworks.com>...
> > Hi,
> >
> > I am trying to find a quick way to cycle through a matrix of size 72x4. However, I would only like to cycle through it every three rows, and not every single row. I have been attempting to get MATLAB to do this but in the for loop it only gives me the 1st three rows and does not continue until the end. So in the end I would like to see 24 3 row data matrices. Here is my code:
> >
> > for j=1:length(x);
> > ind=find(x(av_window));
> > x=x(ind,:);
> >
> > end;
> >
> > where av_window=[1:3]' (number of rows) and x is the 72x4 matrix. There must be a simple way of defining a 3-row window , but it only works for the first three rows and not for the subsequent rows (i.e. 4,5,6 then 7,8,9 up until 72).
> >
> > Please can someone suggest how to go about this problem.
> >
> > Justin.
> First of all, you shouldn't use find like that, find will now returns the non zero elements (their indexes) in the first three rows (see help)
> Second, what you do then is reassign x to a part of the original x, which will leave you with only a part of x, the rest is gone.
>
> But as an answer to your question you could use, for example:
>
> for j=1:size(x,1)/3
> rows=x((3*i-2):3*n);
> end
And with all my mistakes, typos and such, fixed, that would be:
for j=1:floor(size(x,1)/3)
    rows=x((3*j-2):3*j);
end

Subject: Cycling through a matrix

From: Rolf

Date: 26 Oct, 2010 14:37:03

Message: 6 of 10

1 more small mistake... I really need an edit functionality here...
for j=1:floor(size(x,1)/3)
    rows=x((3*j-2):3*j,:);
end

Subject: Cycling through a matrix

From: Justin

Date: 26 Oct, 2010 15:46:04

Message: 7 of 10

Hi Rolf,

I’ve been using your code and thanks very much by the way. However, I forgot to mention (crucially) that each time I move onto a new set of 3 rows, there needs to be an overlap with the previous one. Here is an example:

NaN NaN NaN NaN
-15.95 -1.67 -6.39 -0.01
-5.22 -3.31 35.27 0.04

(first 3 rows of data)

The second row should then be:

-15.95 -1.67 -6.39 -0.01
-5.22 -3.31 35.27 0.04
NaN NaN NaN NaN

(as you can see there is overlap with the previous set of data – since what I am trying to do is calculate a 3-month-moving average (not just a plain 3 month average).

Therefore is there a way that I could cycle through the data and select 3 rows of data, but one row at a time (so that the new 3 set of rows contains only in effect one new row)?

Justin.



"Rolf " <r.vermeer@student.removethis.utwente.nl> wrote in message <ia6p2f$482$1@fred.mathworks.com>...
> 1 more small mistake... I really need an edit functionality here...
> for j=1:floor(size(x,1)/3)
> rows=x((3*j-2):3*j,:);
> end

Subject: Cycling through a matrix

From: Matt J

Date: 26 Oct, 2010 16:03:06

Message: 8 of 10

"Justin " <J.Krijnen@uea.ac.uk> wrote in message <ia6t3s$b56$1@fred.mathworks.com>...
>
> Therefore is there a way that I could cycle through the data and select 3 rows of data, but one row at a time (so that the new 3 set of rows contains only in effect one new row)?
 
Yes, it's called convolution:

NanMap=isnan(x);
x(NanMap)=0;
MovingAverage=conv2(x,[1;1;1],'valid')./conv2(double(NanMap),[1;1;1],'valid');

Subject: Cycling through a matrix

From: Matt J

Date: 26 Oct, 2010 16:17:04

Message: 9 of 10

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <ia6u3q$hrp$1@fred.mathworks.com>...
> "Justin " <J.Krijnen@uea.ac.uk> wrote in message <ia6t3s$b56$1@fred.mathworks.com>...
> >
> > Therefore is there a way that I could cycle through the data and select 3 rows of data, but one row at a time (so that the new 3 set of rows contains only in effect one new row)?
>
> Yes, it's called convolution:
>
> NanMap=isnan(x);
> x(NanMap)=0;
> MovingAverage=conv2(x,[1;1;1],'valid')./conv2(double(NanMap),[1;1;1],'valid');
=============


Whoops. That last line should be


MovingAverage=conv2(x,[1;1;1],'valid')./conv2(double(~NanMap),[1;1;1],'valid');

Subject: Cycling through a matrix

From: Justin

Date: 27 Oct, 2010 10:17:03

Message: 10 of 10

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <ia6uu0$d4m$1@fred.mathworks.com>...
> "Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <ia6u3q$hrp$1@fred.mathworks.com>...
> > "Justin " <J.Krijnen@uea.ac.uk> wrote in message <ia6t3s$b56$1@fred.mathworks.com>...
> > >
> > > Therefore is there a way that I could cycle through the data and select 3 rows of data, but one row at a time (so that the new 3 set of rows contains only in effect one new row)?
> >
> > Yes, it's called convolution:
> >
> > NanMap=isnan(x);
> > x(NanMap)=0;
> > MovingAverage=conv2(x,[1;1;1],'valid')./conv2(double(NanMap),[1;1;1],'valid');
> =============
>
>
> Whoops. That last line should be
>
>
> MovingAverage=conv2(x,[1;1;1],'valid')./conv2(double(~NanMap),[1;1;1],'valid');

Thanks for your help.

Tags for 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