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:
Avoiding the need for intermediate / dummy variables

Subject: Avoiding the need for intermediate / dummy variables

From: AJP

Date: 30 May, 2011 21:13:05

Message: 1 of 8

Say I have a vector of data x and a function f(x).

Now say I want to evaluate f(x) only for x>2, so I do:

ind=x>2;
y=f(x(ind));

Now say I want to find y>5;

ind2=y>5;

Now how can I use this information to get the x values corresponding to the y>5 values, WITHOUT using this crude method where I create an intermediate variable:

x_intermediate = x(ind1); % I want to avoid creating this

x_answer = x_intermediate(ind2);

Subject: Avoiding the need for intermediate / dummy variables

From: ImageAnalyst

Date: 30 May, 2011 21:24:23

Message: 2 of 8

Of course. Just do

y=f(x) % Not f(x(ind))
acceptable_original_indexes = (y > 5) & (x > 2)


Full demo (save as test.m)

function test()
workspace; % Show the workspace panel.
x = 3 * rand(1, 10) % Sample data
y=f(x) % Get y for ALL indexes.
acceptable_original_indexes = (y > 5) & (x > 2)

% Sample function f to multiply x by 6.
function y = f(data)
y = 6 * data;

Subject: Avoiding the need for intermediate / dummy variables

From: ImageAnalyst

Date: 30 May, 2011 21:27:45

Message: 3 of 8

You don't even need the y if you don't want to use it - just pass in
the f(x) instead of y
acceptable_original_indexes = (f(x) > 5) & (x > 2)

You can get the actual indexes, instead of the logical index array by
passing acceptable_original_indexes into find():

actual_indexes = find(acceptable_original_indexes);

Subject: Avoiding the need for intermediate / dummy variables

From: Nasser M. Abbasi

Date: 30 May, 2011 21:34:23

Message: 4 of 8

On 5/30/2011 2:13 PM, AJP wrote:
> Say I have a vector of data x and a function f(x).
>
> Now say I want to evaluate f(x) only for x>2, so I do:
>
> ind=x>2;
> y=f(x(ind));
>
> Now say I want to find y>5;
>
> ind2=y>5;
>
> Now how can I use this information to get the x values corresponding to the y>5 values,
> WITHOUT using this crude method where I create an intermediate variable:
>
> x_intermediate = x(ind1); % I want to avoid creating this
>
> x_answer = x_intermediate(ind2);

In matlab, such indexing type operations need to be applied to an
actual variable

so, one can't write sin(x)(4) for example, but must make a temporary
variable

v=sin(x)
v(4)

I think octave (Matlab clone) might do what you are looking for,
I know it does support sin(x)(4) type operations.

It would be nice if one can do that in Matlab, but I am sure there
is a good technical reason why it is not implemented in Matlab.

--Nasser

Subject: Avoiding the need for intermediate / dummy variables

From: AJP

Date: 30 May, 2011 21:36:02

Message: 5 of 8

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <7a28386f-e0cf-473a-a75f-245d833b97d6@f2g2000yqh.googlegroups.com>...
> Of course. Just do
>
> y=f(x) % Not f(x(ind))
> acceptable_original_indexes = (y > 5) & (x > 2)
>


Evaluating f(x) instead of f(x(ind)) is not a good solution as in my case the function f takes a long time to evaluate, hence the reason why I wanted to only evaluate it for specific x values.

Subject: Avoiding the need for intermediate / dummy variables

From: AJP

Date: 30 May, 2011 21:39:04

Message: 6 of 8

Walter Robinson suggested this nice solution on another board:

ind1 = find(x>2);
y = f(x(ind1));
x(ind1(y>5))

So thanks to him for this.

Subject: Avoiding the need for intermediate / dummy variables

From: AJP

Date: 30 May, 2011 21:53:04

Message: 7 of 8

"AJP" wrote in message <is12po$7id$1@newscl01ah.mathworks.com>...
> Walter Robinson suggested this nice solution on another board:
>
> ind1 = find(x>2);
> y = f(x(ind1));
> x(ind1(y>5))
>
> So thanks to him for this.

Actually, I am mistaken. Walter's solution does not work as this relies on being able to use find() to generate the first set of indices.

I cannot do this as I need to use a logical operation (i.e. inpolygon) to find x,y values where I am going to evaluate f(x,y). I only used the simple 1D example of x>2 and f(x) above to save explaining the whole problem!

Nasser writes exactly what I require!

I want to do operations like x(ind1)(ind2) but this apparently not possible.

Anyone have any other ideas on this?

Subject: Avoiding the need for intermediate / dummy variables

From: AJP

Date: 30 May, 2011 22:05:05

Message: 8 of 8

 Ha - I'm being stupid!

I can simply use find(<<logical test>>) to get the indices initially, then use Walter suggestion.

Thanks everyone for your input.

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