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:
conditional maximum in a matrix

Subject: conditional maximum in a matrix

From: Priom Rahman

Date: 8 Sep, 2010 21:07:06

Message: 1 of 13

Hi guys

I was wondering if someone could help me come up with a code for my problem.

I have a matrix for example

M = [ 1 2 1 2 6 5 4 2; 1 6 2 3 4 1 5 6; 1 2 5 6 7 8 1 2; 1 1 1 7 2 3 5 6; 5 1 1 32 1 3 5 2; 32 1 2 1 1 2 4 6]

M =

     1 2 1 2 6 5 4 2
     1 6 2 3 4 1 5 6
     1 2 5 6 7 8 1 2
     1 1 1 7 2 3 5 6
     5 1 1 32 1 3 5 2
    32 1 2 1 1 2 4 6


I want to find the maximum but for a 3 cell period. I want to go across the columns and then down the rows.

So in this case the maximum for the 3 cell would be (2+32+1 = 35) ie the end of row 5 and then the start of row 6.

once I know the maximum I want to know the row number (so row 5 and 6 - often it will only happen in the one row and not carry over)

I need to do this for a matrix thats 15000 x 244 and I need a window of (20 cells - ie not 3) - but I just wanted to know how to do it for a small matrix so I can apply it for a larger one.

Thanks for your help. I'm new with coding so I'm a little stuck.

Subject: conditional maximum in a matrix

From: Sean

Date: 8 Sep, 2010 21:23:20

Message: 2 of 13

"Priom Rahman" <z3188254@unsw.edu.au> wrote in message <i68ttq$m96$1@fred.mathworks.com>...
> Hi guys
>
> I was wondering if someone could help me come up with a code for my problem.
>
> I have a matrix for example
>
> M = [ 1 2 1 2 6 5 4 2; 1 6 2 3 4 1 5 6; 1 2 5 6 7 8 1 2; 1 1 1 7 2 3 5 6; 5 1 1 32 1 3 5 2; 32 1 2 1 1 2 4 6]
>
> M =
>
> 1 2 1 2 6 5 4 2
> 1 6 2 3 4 1 5 6
> 1 2 5 6 7 8 1 2
> 1 1 1 7 2 3 5 6
> 5 1 1 32 1 3 5 2
> 32 1 2 1 1 2 4 6
>
>
> I want to find the maximum but for a 3 cell period. I want to go across the columns and then down the rows.
>
> So in this case the maximum for the 3 cell would be (2+32+1 = 35) ie the end of row 5 and then the start of row 6.
>
> once I know the maximum I want to know the row number (so row 5 and 6 - often it will only happen in the one row and not carry over)
>
> I need to do this for a matrix thats 15000 x 244 and I need a window of (20 cells - ie not 3) - but I just wanted to know how to do it for a small matrix so I can apply it for a larger one.
>
> Thanks for your help. I'm new with coding so I'm a little stuck.

First off: wouldn't the maximum be 39 i.e. (5+2+32) = 39?

Do you have the image processing toolbox?

If so then this will do it:
Mt = M.';
[my_max, idx] = max(imfilter(reshape(Mt,[],1),ones(3,1)));
Mt(idx-1:idx+1)
%my_max will be the value, the last line are the three components, and idx-1:idx+1 = the linear indices into Mt.
%To extend this to 20 cells change the 'ones(3,1) to ones(20,1)'

Subject: conditional maximum in a matrix

From: Ross W

Date: 8 Sep, 2010 21:28:04

Message: 3 of 13

"Priom Rahman" <z3188254@unsw.edu.au> wrote in message <i68ttq$m96$1@fred.mathworks.com>...
> Hi guys
>
> I was wondering if someone could help me come up with a code for my problem.
>
> I have a matrix for example
>
> M = [ 1 2 1 2 6 5 4 2; 1 6 2 3 4 1 5 6; 1 2 5 6 7 8 1 2; 1 1 1 7 2 3 5 6; 5 1 1 32 1 3 5 2; 32 1 2 1 1 2 4 6]
>
> M =
>
> 1 2 1 2 6 5 4 2
> 1 6 2 3 4 1 5 6
> 1 2 5 6 7 8 1 2
> 1 1 1 7 2 3 5 6
> 5 1 1 32 1 3 5 2
> 32 1 2 1 1 2 4 6
>
>
> I want to find the maximum but for a 3 cell period. I want to go across the columns and then down the rows.
>
> So in this case the maximum for the 3 cell would be (2+32+1 = 35) ie the end of row 5 and then the start of row 6.
>
> once I know the maximum I want to know the row number (so row 5 and 6 - often it will only happen in the one row and not carry over)
>
> I need to do this for a matrix thats 15000 x 244 and I need a window of (20 cells - ie not 3) - but I just wanted to know how to do it for a small matrix so I can apply it for a larger one.
>
> Thanks for your help. I'm new with coding so I'm a little stuck.

hi

if you are new to coding, then you need to break the problem into subproblems, and then solve them one at a time.

here are some ideas that might help.

%make a 1-column vector of all the numbers, in the order you want them processed. (use transpose and colon)

%make a 1-column vector with all possible sums of 3 adjacent values (e.g. use filter). be clear about how you deal with the ends of the vector.

%find the maximum value and its location (use max)

%convert the location in that long vector back into row-column location (use ind2sub)

try some of these and get back to the list with questions

Ross

Subject: conditional maximum in a matrix

From: Sean

Date: 8 Sep, 2010 21:51:19

Message: 4 of 13

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i68us8$ps8$1@fred.mathworks.com>...
> "Priom Rahman" <z3188254@unsw.edu.au> wrote in message <i68ttq$m96$1@fred.mathworks.com>...
> > Hi guys
> >
> > I was wondering if someone could help me come up with a code for my problem.
> >
> > I have a matrix for example
> >
> > M = [ 1 2 1 2 6 5 4 2; 1 6 2 3 4 1 5 6; 1 2 5 6 7 8 1 2; 1 1 1 7 2 3 5 6; 5 1 1 32 1 3 5 2; 32 1 2 1 1 2 4 6]
> >
> > M =
> >
> > 1 2 1 2 6 5 4 2
> > 1 6 2 3 4 1 5 6
> > 1 2 5 6 7 8 1 2
> > 1 1 1 7 2 3 5 6
> > 5 1 1 32 1 3 5 2
> > 32 1 2 1 1 2 4 6
> >
> >
> > I want to find the maximum but for a 3 cell period. I want to go across the columns and then down the rows.
> >
> > So in this case the maximum for the 3 cell would be (2+32+1 = 35) ie the end of row 5 and then the start of row 6.
> >
> > once I know the maximum I want to know the row number (so row 5 and 6 - often it will only happen in the one row and not carry over)
> >
> > I need to do this for a matrix thats 15000 x 244 and I need a window of (20 cells - ie not 3) - but I just wanted to know how to do it for a small matrix so I can apply it for a larger one.
> >
> > Thanks for your help. I'm new with coding so I'm a little stuck.
>
> First off: wouldn't the maximum be 39 i.e. (5+2+32) = 39?
>
> Do you have the image processing toolbox?
>
> If so then this will do it:
> Mt = M.';
> [my_max, idx] = max(imfilter(reshape(Mt,[],1),ones(3,1)));
> Mt(idx-1:idx+1)
> %my_max will be the value, the last line are the three components, and idx-1:idx+1 = the linear indices into Mt.
> %To extend this to 20 cells change the 'ones(3,1) to ones(20,1)'




If you don't have the IPT here's another way to do it. This way is probably better as far as speed anyway:

%%%%
Mt = M.';
cell_size = 3; %set your window size (can be any number, the below code is generalized for it)
cs = cumsum(Mt(:)); %cumulative sum
[~,idx] = max(cs - [zeros(cell_size-1,1);c(1:end-(cell_size-1))]); %biggest difference over the cell_size in the cumulative sum (cell_size - 1 since it's inclusive)
Mt(idx-(cell_size-1):idx)

%SCd

Subject: conditional maximum in a matrix

From: Priom Rahman

Date: 8 Sep, 2010 22:17:04

Message: 5 of 13

Hi Ross,

I have tried breaking it down and creating a single vector, but the problem is once I find the maximum. I have trouble relating it back to the initial matrix.
I need to do this because I have date vectors associated with each row that talk to my initial matrix which I need to know.

Thats why I was posting to see if there was a way I could do this by leaving the matrix intact.

But thank you for reassuring me that I was on the right track at least.

Regards

Subject: conditional maximum in a matrix

From: Priom Rahman

Date: 8 Sep, 2010 22:30:24

Message: 6 of 13

Dear Sean,

I just sent a msg but I dont know if it went through,

I do have the image processing toolbox I just checked. But as per your post before, the maximum is 35 not 38 because I need to go across then down.

The matrix is a represention of fragmented rainfall, where each row is a different day. I need to find the maximum for a window of say (3 hours) which is why I need it to carry over to the next day. I hope that puts things into context.

Thank you very much for your help.

Regards

Subject: conditional maximum in a matrix

From: Ross W

Date: 8 Sep, 2010 22:47:05

Message: 7 of 13

"Priom Rahman" <z3188254@unsw.edu.au> wrote in message <i69210$j6b$1@fred.mathworks.com>...
> Hi Ross,
>
> I have tried breaking it down and creating a single vector, but the problem is once I find the maximum. I have trouble relating it back to the initial matrix.
> I need to do this because I have date vectors associated with each row that talk to my initial matrix which I need to know.
>
> Thats why I was posting to see if there was a way I could do this by leaving the matrix intact.
>
> But thank you for reassuring me that I was on the right track at least.
>
> Regards

Hi

ind2sub is the function you need to convert from the location in the vector, to the location in the matrix (not sub2ind as i said in my post).

doc ind2sub has a nice example explaining it.

Ross

Subject: conditional maximum in a matrix

From: Priom Rahman

Date: 8 Sep, 2010 23:50:09

Message: 8 of 13

Dear Sean,

Just saw what you meant about the 39. My mistake. And your code worked brilliantly.

Thank you very much !

Subject: conditional maximum in a matrix

From: Matt Fig

Date: 9 Sep, 2010 02:15:26

Message: 9 of 13

> If you don't have the IPT here's another way to do it. This way is probably better as far as speed anyway:
>
> %%%%
> Mt = M.';
> cell_size = 3; %set your window size (can be any number, the below code is generalized for it)
> cs = cumsum(Mt(:)); %cumulative sum
> [~,idx] = max(cs - [zeros(cell_size-1,1);c(1:end-(cell_size-1))]); %biggest difference over the cell_size in the cumulative sum (cell_size - 1 since it's inclusive)
> Mt(idx-(cell_size-1):idx)
>
> %SCd

If I understand the problem correctly, this will not give the correct solution sometimes. For example:

M = [1 1 1;9 9 9;2 2 2];

Sean's code gives the max sum as 19. Also, this one errors:

M = [1000 1 -13;9 9 9;2 2 2]

Here is how I would do it:

Mt = M.';
[m,Loc] = max(filter(ones(1,3)/3,1,Mt(:).').');
sum(Mt(max(1,Loc-2):Loc)) %

Subject: conditional maximum in a matrix

From: Matt Fig

Date: 9 Sep, 2010 02:27:23

Message: 10 of 13

"Matt Fig" <spamanon@yahoo.com> wrote in message
> Here is how I would do it:
>
> Mt = M.';
> [m,Loc] = max(filter(ones(1,3)/3,1,Mt(:).').');
> sum(Mt(max(1,Loc-2):Loc)) %

I should have taken the two transpositions out of the second line. I put these there early on and forgot to take them out, as they are not necessary and slow down the code. So it should be:

Mt = M.';
[Loc,Loc] = max(filter(ones(1,3)/3,1,Mt(:))); % Replace 3 by your "cell" size.
sum(Mt(max(1,Loc-2):Loc)) % Replace 2 by your "cell" size -1.

Subject: conditional maximum in a matrix

From: Priom Rahman

Date: 9 Sep, 2010 03:33:25

Message: 11 of 13

Dear Matt,

That was very very vigil of you to pick up. I tried Sean's code with a few random matrices and it worked, so I included it my loops.

Your code seems to go that extra step, but since i dont have any negative data and my maximum values fall within days and not accross boundaries, Sean's code was okay for my purposes and yeiled the same results as yours "phew" !!

I was already onto doing other things with the data outputs I got this morning ! and thankfull I didnt have to go and redo them

But I will make a mental note for next time I have to do something.

Thanks for all your help.

Subject: conditional maximum in a matrix

From: Oleg Komarov

Date: 9 Sep, 2010 07:52:05

Message: 12 of 13

You can verticalize you dataset mantaining the descriptive features. So if:
M = [ 1 2 1 2 6 5 4 2; 1 6 2 3 4 1 5 6; 1 2 5 6 7 8 1 2; 1 1 1 7 2 3 5 6; 5 1 1 32 1 3 5 2; 32 1 2 1 1 2 4 6];
You can add the datenum on the left and the number of the column on the top:
M = [ [NaN 1:size(M,2)]; [(734378:734383).' M]]
M =
         NaN 1 2 3 4 5 6 7 8
      734378 1 2 1 2 6 5 4 2
      734379 1 6 2 3 4 1 5 6
      734380 1 2 5 6 7 8 1 2
      734381 1 1 1 7 2 3 5 6
      734382 5 1 1 32 1 3 5 2
      734383 32 1 2 1 1 2 4 6

Then you can unPivot it:
unPivot(M,2) % on the Fex Pivot/unPivot
ans =
           1 734378 1
           2 734378 2
           3 734378 1
           4 734378 2
           5 734378 6
           6 734378 5
           7 734378 4
           8 734378 2
           1 734379 1
           2 734379 6
           3 734379 2
           4 734379 3
           5 734379 4
           6 734379 1
           7 734379 5
           8 734379 6
           1 734380 1
           2 734380 2
           3 734380 5
           4 734380 6
           5 734380 7
           6 734380 8
           7 734380 1
           8 734380 2
           1 734381 1
           2 734381 1
           3 734381 1
           4 734381 7
           5 734381 2
           6 734381 3
           7 734381 5
     ...

Then you can apply the solutions proposed above (without the reshaping) while being able to "read" the dataset. (1st col are the column headers of the modified M, 2nd col are the dates as serial numbers and 3rd col are the rainfall data).

Oleg

Subject: conditional maximum in a matrix

From: Priom Rahman

Date: 9 Sep, 2010 09:02:08

Message: 13 of 13

Dear Oleg,

Thats very interesting. Learning something new with every post !!

Thanks everyone for all 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