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:
How do I reduce this to one line from two lines?

Subject: How do I reduce this to one line from two lines?

From: Ulrik Nash

Date: 15 Jul, 2010 10:47:05

Message: 1 of 10

Hi Everyone,

I it possible to reduce the following to a single line?

A = max([firm_profit(:,t),firm_price(:,t)])
B = A(1,1)

Kind regards,

Ulrik.

Subject: How do I reduce this to one line from two lines?

From: us

Date: 15 Jul, 2010 10:56:04

Message: 2 of 10

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <i1mov9$m1f$1@fred.mathworks.com>...
> Hi Everyone,
>
> I it possible to reduce the following to a single line?
>
> A = max([firm_profit(:,t),firm_price(:,t)])
> B = A(1,1)
>
> Kind regards,
>
> Ulrik.

yes, of course...

     A=max([firm_profit(:,t),firm_price(:,t)]);B=A(1,1);

us

Subject: How do I reduce this to one line from two lines?

From: Ulrik Nash

Date: 15 Jul, 2010 11:02:05

Message: 3 of 10

"us " <us@neurol.unizh.ch> wrote in message <i1mpg4$p36$1@fred.mathworks.com>...
> "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <i1mov9$m1f$1@fred.mathworks.com>...
> > Hi Everyone,
> >
> > I it possible to reduce the following to a single line?
> >
> > A = max([firm_profit(:,t),firm_price(:,t)])
> > B = A(1,1)
> >
> > Kind regards,
> >
> > Ulrik.
>
> yes, of course...
>
> A=max([firm_profit(:,t),firm_price(:,t)]);B=A(1,1);
>
> us

That is neat! Thanks us! :-)

Subject: How do I reduce this to one line from two lines?

From: Ulrik Nash

Date: 15 Jul, 2010 11:09:05

Message: 4 of 10

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <i1mprd$hqs$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <i1mpg4$p36$1@fred.mathworks.com>...
> > "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <i1mov9$m1f$1@fred.mathworks.com>...
> > > Hi Everyone,
> > >
> > > I it possible to reduce the following to a single line?
> > >
> > > A = max([firm_profit(:,t),firm_price(:,t)])
> > > B = A(1,1)
> > >
> > > Kind regards,
> > >
> > > Ulrik.
> >
> > yes, of course...
> >
> > A=max([firm_profit(:,t),firm_price(:,t)]);B=A(1,1);
> >
> > us
>
> That is neat! Thanks us! :-)


Maybe I was a little fast there. Is it not possible to achieve the result with only one equation?

Subject: How do I reduce this to one line from two lines?

From: us

Date: 15 Jul, 2010 11:20:05

Message: 5 of 10

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <i1mq8h$e13$1@fred.mathworks.com>...
> "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <i1mprd$hqs$1@fred.mathworks.com>...
> > "us " <us@neurol.unizh.ch> wrote in message <i1mpg4$p36$1@fred.mathworks.com>...
> > > "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <i1mov9$m1f$1@fred.mathworks.com>...
> > > > Hi Everyone,
> > > >
> > > > I it possible to reduce the following to a single line?
> > > >
> > > > A = max([firm_profit(:,t),firm_price(:,t)])
> > > > B = A(1,1)
> > > >
> > > > Kind regards,
> > > >
> > > > Ulrik.
> > >
> > > yes, of course...
> > >
> > > A=max([firm_profit(:,t),firm_price(:,t)]);B=A(1,1);
> > >
> > > us
> >
> > That is neat! Thanks us! :-)
>
>
> Maybe I was a little fast there. Is it not possible to achieve the result with only one equation?

well... the first reply was a ...JOKE..., of course...

one of the solutions

     [a,b]=deal(max(-1:2))
% a = 2
% b = 2

us

Subject: How do I reduce this to one line from two lines?

From: Ulrik Nash

Date: 15 Jul, 2010 11:29:05

Message: 6 of 10

"us " <us@neurol.unizh.ch> wrote in message <i1mqt5$obr$1@fred.mathworks.com>...
> "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <i1mq8h$e13$1@fred.mathworks.com>...
> > "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <i1mprd$hqs$1@fred.mathworks.com>...
> > > "us " <us@neurol.unizh.ch> wrote in message <i1mpg4$p36$1@fred.mathworks.com>...
> > > > "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <i1mov9$m1f$1@fred.mathworks.com>...
> > > > > Hi Everyone,
> > > > >
> > > > > I it possible to reduce the following to a single line?
> > > > >
> > > > > A = max([firm_profit(:,t),firm_price(:,t)])
> > > > > B = A(1,1)
> > > > >
> > > > > Kind regards,
> > > > >
> > > > > Ulrik.
> > > >
> > > > yes, of course...
> > > >
> > > > A=max([firm_profit(:,t),firm_price(:,t)]);B=A(1,1);
> > > >
> > > > us
> > >
> > > That is neat! Thanks us! :-)
> >
> >
> > Maybe I was a little fast there. Is it not possible to achieve the result with only one equation?
>
> well... the first reply was a ...JOKE..., of course...
>
> one of the solutions
>
> [a,b]=deal(max(-1:2))
> % a = 2
> % b = 2
>
> us

Haha, and I fell, no, jumped right in :-) Be nice to beginners! ;-) anyway, dare I say thanks this time?

Subject: How do I reduce this to one line from two lines?

From: Jan Simon

Date: 15 Jul, 2010 11:38:05

Message: 7 of 10

Dear Ulrik,

> I it possible to reduce the following to a single line?
> A = max([firm_profit(:,t),firm_price(:,t)])
> B = A(1,1)

I do not see a benefit in redusing the number of lines.
If you want to have the variables A and B, it is not possible. But if you want just B to be the maximum of the first column, you can omit the 2nd column:
  B = max(firm_profit(:, t));
Are you sure, that this is what you want to get?

Jan

Subject: How do I reduce this to one line from two lines?

From: Ulrik Nash

Date: 15 Jul, 2010 12:23:07

Message: 8 of 10

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <i1mrus$1k6$1@fred.mathworks.com>...
> Dear Ulrik,
>
> > I it possible to reduce the following to a single line?
> > A = max([firm_profit(:,t),firm_price(:,t)])
> > B = A(1,1)
>
> I do not see a benefit in redusing the number of lines.
> If you want to have the variables A and B, it is not possible. But if you want just B to be the maximum of the first column, you can omit the 2nd column:
> B = max(firm_profit(:, t));
> Are you sure, that this is what you want to get?
>
> Jan

Hi Jan,

What I want to achieve is an isolation of the price at which profit is greatest. I was hoping this could be done in a single line, to get a single value.

Ulrik.

Subject: How do I reduce this to one line from two lines?

From: dpb

Date: 15 Jul, 2010 14:28:43

Message: 9 of 10

Ulrik Nash wrote:
> "Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message
> <i1mrus$1k6$1@fred.mathworks.com>...
>> Dear Ulrik,
>>
>> > I it possible to reduce the following to a single line?
>> > A = max([firm_profit(:,t),firm_price(:,t)])
>> > B = A(1,1)
>>
>> I do not see a benefit in redusing the number of lines.
>> If you want to have the variables A and B, it is not possible. But if
>> you want just B to be the maximum of the first column, you can omit
>> the 2nd column:
>> B = max(firm_profit(:, t));
>> Are you sure, that this is what you want to get?
>>
>> Jan
>
> Hi Jan,
> What I want to achieve is an isolation of the price at which profit is
> greatest. I was hoping this could be done in a single line, to get a
> single value.
...

Look at the optional return values of max()

doc max

Still need two steps afaict. Otherwise, the locations of the two max
values aren't correlated. You've returned each of the maximum of price
_and_ maximum profit, not necessarily the price for max profit as is
(unless, of course, there's nothing going on in the data other than a
higher price produces a higher profit in which case there's nothing to
learn from the data anyway as it's in/from a region of totally elastic
price/volume relationship).

--

Subject: How do I reduce this to one line from two lines?

From: Jan Simon

Date: 15 Jul, 2010 16:28:22

Message: 10 of 10

Dear Ulrik,

> > > A = max([firm_profit(:,t),firm_price(:,t)])
> > > B = A(1,1)

> What I want to achieve is an isolation of the price at which profit is greatest. I was hoping this could be done in a single line, to get a single value.

Aha (sorry, I cannot translate this). This is something different. Did you try to run the code you've posted??

Here is your desired one-liner:
  B = price(profit(:, t) == max(profit(:, t)), t);
I've omitted the redundant "firm_".
Of course this is less efficient than using the 2nd output of the MAX command to get the index of the greatest element:
  [dum, index] = max(profit(:, t));
  B = price(index, t);
Anyway, you have to care for multiple or empty max values (if "profit" is NaN...).

Kind regards, Jan

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