Thread Subject: Substitute values in data array

Subject: Substitute values in data array

From: Florian

Date: 4 Nov, 2009 11:57:01

Message: 1 of 4

Dear all,
I want to substitute values in data array and there is a matlab function in the toolbox called changem to o so.
However, this function uses a loop:

for k = 1:numel(newval)
    B(A == oldval(k)) = newval(k);
end

and my array is very large. I have tried to speed it up several ways, but no success. Does anyone have an idea how this could be done faster?

cheers,
flo

Subject: Substitute values in data array

From: Jan Simon

Date: 4 Nov, 2009 12:37:02

Message: 2 of 4

Dear Florian!

> I want to substitute values in data array and there is a matlab function in the toolbox called changem to o so.
> However, this function uses a loop:
>
> for k = 1:numel(newval)
> B(A == oldval(k)) = newval(k);
> end
>
> and my array is very large.

Please specify the sizes auf B, A, newval.

> I have tried to speed it up several ways, but no success. Does anyone have an idea how this could be done faster?

Please tell us, what you have tried already. Then we do not have to try it again...

Sometimes FINDSTR is faster than the == operator.
Are there repititions in oldval/newval?
Have you tried to use backwar-indexing (e.g. X(oldval)=newval; B = X(A);), as e.g. in IND2RGB?

Good luck, Jan

Subject: Substitute values in data array

From: Bruno Luong

Date: 4 Nov, 2009 12:55:05

Message: 3 of 4

"Florian" <f.pappenberger@NOSPAMlancater.ac.uk> wrote in message <hcrq6d$mn1$1@fred.mathworks.com>...
> Dear all,
> I want to substitute values in data array and there is a matlab function in the toolbox called changem to o so.
> However, this function uses a loop:
>
> for k = 1:numel(newval)
> B(A == oldval(k)) = newval(k);
> end
>

Careful if oldval and newval intersect and arranged in the order that can fail your for-loop.

A=ceil(10*rand(1,12))
oldval=[1 2];
newval=-oldval

B = A;
[tf k]=ismember(A,oldval);
B(tf)=newval(k(tf))

Bruno

Subject: Substitute values in data array

From: Florian

Date: 4 Nov, 2009 13:49:02

Message: 4 of 4

Bruno,
thanks that works - you should submit that also to mathworks as a replacement for changem .m :-)

Thanks a lot!!!!!
flo

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com