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:
create matrix A with max value from matrix B

Subject: create matrix A with max value from matrix B

From: Robert Orzechowski

Date: 20 May, 2010 22:55:05

Message: 1 of 9

I would like to create new 3D matrix (let's call it A) from 3D matrix B.
Matrix A should have all zero elements apart from the max element in matrix B (at the same indices).

example:
B(:,:,1)=[ 1 2; 3 4] (find here the max value)
B(:,:,2)=[7 8 ;5 6] (find here the max value)
B(:,:,3)=[12 10 ;11 9] (find here the max value)

after this new function I would like to get A like:

A(:,:,1)=[ 0 0; 0 4]
A(:,:,1)=[ 0 8; 0 0]
A(:,:,1)=[ 12 0; 0 0]

Could you help me, to write down such function ?
Thank you in advance
Robert

Subject: create matrix A with max value from matrix B

From: Walter Roberson

Date: 20 May, 2010 23:36:04

Message: 2 of 9

"Robert Orzechowski" <orzechowskirobert@yahoo.com> wrote in message <ht4ek9$4g4$1@fred.mathworks.com>...
> I would like to create new 3D matrix (let's call it A) from 3D matrix B.
> Matrix A should have all zero elements apart from the max element in matrix B (at the same indices).

> example:
> B(:,:,1)=[ 1 2; 3 4] (find here the max value)
> B(:,:,2)=[7 8 ;5 6] (find here the max value)
> B(:,:,3)=[12 10 ;11 9] (find here the max value)
 
> after this new function I would like to get A like:
 
> A(:,:,1)=[ 0 0; 0 4]
> A(:,:,1)=[ 0 8; 0 0]
> A(:,:,1)=[ 12 0; 0 0]

max(max(B,[],2))

will give you the appropriate values, Finding the appropriate location takes more work. (Oh, look at that, time to head home ;-) )

Subject: create matrix A with max value from matrix B

From: Robert Orzechowski

Date: 21 May, 2010 08:49:08

Message: 3 of 9

"Walter Roberson" <roberson@ibd.nrc-cnrc.gc.ca> wrote in message <ht4h14$5fa$1@fred.mathworks.com>...
> "Robert Orzechowski" <orzechowskirobert@yahoo.com> wrote in message <ht4ek9$4g4$1@fred.mathworks.com>...
> > I would like to create new 3D matrix (let's call it A) from 3D matrix B.
> > Matrix A should have all zero elements apart from the max element in matrix B (at the same indices).
>
> max(max(B,[],2))
> will give you the appropriate values, Finding the appropriate location takes more work. (Oh, look at that, time to head home ;-) )


of course output should be :
> > A(:,:,1)=[ 0 0; 0 4]
> > A(:,:,2)=[ 0 8; 0 0]
> > A(:,:,3)=[ 12 0; 0 0]

I wonder, how to find indices (location). Sould be done using max returning two arguments or there is any other way ?
My taks could be also not create additional matrix, but also I can modify B...

Thanks for any help
Robert

Subject: create matrix A with max value from matrix B

From: tinne123

Date: 21 May, 2010 11:23:04

Message: 4 of 9

Hi Robert,

may I just ask do you really need to come up with zeros everywhere else around the max value? What is the goal of such step? It happenned to my a few times that I tried to raise a concrete question on this (or another) forum and to that end I gave a "simplifying example", which was however, out of the context. After a bit of communication with some strong programmers, it came out that I should view the problem differently and don't really need what I asked about. Maybe you could explain where do you want to go with that step.

Good luck!

Subject: create matrix A with max value from matrix B

From: Robert Orzechowski

Date: 21 May, 2010 13:23:05

Message: 5 of 9

"tinne123 " <nastanova@yahoo.co.uk> wrote in message <ht5qeo$25v$1@fred.mathworks.com>...
> Hi Robert,
>
> may I just ask do you really need to come up with zeros everywhere else around the max value? What is the goal of such step? It happenned to my a few times that I tried to raise a concrete question on this (or another) forum and to that end I gave a "simplifying example", which was however, out of the context. After a bit of communication with some strong programmers, it came out that I should view the problem differently and don't really need what I asked about. Maybe you could explain where do you want to go with that step.
>
> Good luck!

I totally agree with you, however I analysed my programm, and I need those values to be zero or INF (but I think this is the same solution in this case).
Those zeros will save computations time in later processing (I won't need then any ifs, elses and other conditions). I need it for the antenna selection simulation.
This function I need to put inside of my already big programm, and I wanted to do it as best as possible (to do not CPU consuming functions).
I think this is strange, that matrix oriented environment
Thank you!

Subject: create matrix A with max value from matrix B

From: tinne123

Date: 21 May, 2010 13:50:20

Message: 6 of 9

Hi Robert,

for your purpose, I think, trying to "locate" the max by calling some dimensions would just complicate things. Your overall code is big anyway, so you want something simple. Yet I am afraid you cannot avoid a couple of loops to solve this problem.

How about creating a function of the type (A) = function (B), so that inside it would do something like this
-----------------------------------------
d = [1, 2; 3, 4];
numelem = 4;
storemax = max(max(d,[],2));
for i = 1:numelem
    if d(i) == storemax
    else
        d(i) = 0;
    end
end
-----------------------------------------
Sorry, I am not strong in programming, but for me these few lines resulted in a matrix d = [0,0;0,4]. Of course I don't know how large your matrices really are etc...

Subject: create matrix A with max value from matrix B

From: Sean

Date: 21 May, 2010 14:17:20

Message: 7 of 9

"Robert Orzechowski" <orzechowskirobert@yahoo.com> wrote in message <ht4ek9$4g4$1@fred.mathworks.com>...
> I would like to create new 3D matrix (let's call it A) from 3D matrix B.
> Matrix A should have all zero elements apart from the max element in matrix B (at the same indices).
>
> example:
> B(:,:,1)=[ 1 2; 3 4] (find here the max value)
> B(:,:,2)=[7 8 ;5 6] (find here the max value)
> B(:,:,3)=[12 10 ;11 9] (find here the max value)
>
> after this new function I would like to get A like:
>
> A(:,:,1)=[ 0 0; 0 4]
> A(:,:,1)=[ 0 8; 0 0]
> A(:,:,1)=[ 12 0; 0 0]
>
> Could you help me, to write down such function ?
> Thank you in advance
> Robert



This was kind of fun!
%%%
%Your B matrix
B(:,:,1)=[ 1 2; 3 4];
B(:,:,2)=[7 8 ;5 6];
B(:,:,3)=[12 10 ;11 9];

%Preallocate zeros for your A matrix
A = zeros(size(B));

%subtract the max off of each of third dimension so that the places with the max in each dimension are zero.
idx = B - repmat(reshape(squeeze(max(max(B,[],2))),[1 1 size(B,3)]), [size(B,1) size(B,2)]);

%Set the indexes equal to their value in B
A(idx==0) = B(idx==0);

Subject: create matrix A with max value from matrix B

From: Robert Orzechowski

Date: 21 May, 2010 14:22:04

Message: 8 of 9

"tinne123 " <nastanova@yahoo.co.uk> wrote in message > How about creating a function of the type (A) = function (B), so that inside it would do something like this
> -----------------------------------------
> d = [1, 2; 3, 4];
> numelem = 4;
> storemax = max(max(d,[],2));
> for i = 1:numelem
> if d(i) == storemax
> else
> d(i) = 0;
> end
> end
> -----------------------------------------

This is what I was afraid of - loop :) I wrote the same, but I was wondering if there is any other way.
BTW my matrices are 2x2x9912, so I have 9912 matrices like your matrix d. In each of them I need to find maximum value and keep it. Unfortunately I have several millions of them...

Thanks,
Robert

Subject: create matrix A with max value from matrix B

From: Jos (10584)

Date: 21 May, 2010 20:35:19

Message: 9 of 9

"Robert Orzechowski" <orzechowskirobert@yahoo.com> wrote in message <ht4ek9$4g4$1@fred.mathworks.com>...
> I would like to create new 3D matrix (let's call it A) from 3D matrix B.
> Matrix A should have all zero elements apart from the max element in matrix B (at the same indices).
>
> example:
> B(:,:,1)=[ 1 2; 3 4] (find here the max value)
> B(:,:,2)=[7 8 ;5 6] (find here the max value)
> B(:,:,3)=[12 10 ;11 9] (find here the max value)
>
> after this new function I would like to get A like:
>
> A(:,:,1)=[ 0 0; 0 4]
> A(:,:,1)=[ 0 8; 0 0]
> A(:,:,1)=[ 12 0; 0 0]
>
> Could you help me, to write down such function ?
> Thank you in advance
> Robert

Here is a vectorized approach:
% the data
  B(:,:,1)=[ 1 2; 3 4] ;
  B(:,:,2)=[7 8 ;5 6] ;
  B(:,:,3)=[12 10 ;11 9]

% engine
  C = reshape(B,[],size(B,3)) % shape in the proper form
  [mB,ix] = max(C,[],1) % find max in each 2-by-2 subarray
  C(:) = 0 % re-set to zeros
  C(sub2ind(size(C), ix, 1:size(C,2))) = mB % fill in the values at the right locations
  C = reshape(C,size(B)) % re-shape into the old shape
% voila!
hth
Jos

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