×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Relation operation between two arrays

Relation operation between two arrays

Relation operation between two arrays

(OP)
Hi,

Im trying to compare two arrays - I want to compare each element separately to the corresponding element in the other array and use the answer as part of an if statement. This is what im doing so far that doesnt work:

if PeakEnv >=level
gain = (level/PeakEnv)^0.75
else
gain = 1
end

have also tried putting if PeakEnv(1:88200)>=level but that doesnt work either. it evaluates the whole array and then makes the condition false for the whole thing.

have done the thing with for loops and it works fine but takes ages.

any suggestions??

thanks
jen

RE: Relation operation between two arrays

Use logical indexing (it's VERY powereful).  Note also my use of ./ and .^ to do element-wise operations.

idx=peakEnv>=level;

gain(idx)=(level(idx)./PeakEnv(idx)).^0.75;
gain(~idx)=1;

RE: Relation operation between two arrays

% Assuming level is a scalar
gain = ones( size( PeakEnv ) );  % malloc and part of work
indexOverLevel = find( PeakEnv >= level );
gain( indexOverLevel ) = ( level / PeakEnv( indexOverLevel ) ) .^ 0.75

% Notice the .^ instead of just ^
That is for point by point instead of a matrix exponential.

RE: Relation operation between two arrays

% OOps! forgot the ./ also instead of the / only

% Assuming level is a scalar
gain = ones( size( PeakEnv ) );  % malloc and part of work
indexOverLevel = find( PeakEnv >= level );
gain( indexOverLevel ) = ( level ./ PeakEnv( indexOverLevel ) ) .^ 0.75

% Notice the .^ instead of just ^
That is for point by point instead of a matrix exponential.

RE: Relation operation between two arrays

VisiGoth,

There's no need for the find() call in your code.  It just adds a little overhead.

RE: Relation operation between two arrays

SometingGuy,
Thanks!  I learned something new.  I didn't know you could say idx=peakEnv>=level;


Also, I didn't know you had already answered this.  I did not see your post when I sent mine.  There must have been a slight delay

RE: Relation operation between two arrays

Quote:

Thanks!  I learned something new.

It's a dull day when I don't learn something new!

RE: Relation operation between two arrays

(OP)
cheers guys.

works a treat.

jenxx

RE: Relation operation between two arrays

(OP)
Im trying to use logical indexing for a similar situation: it requires the comparison of a vector with another vector which is then changed depending on whether the condition is true. I want the next element of the vector to be compared to this new value and this carries on...

Because the logical indexing above compares the vectors that dont change im kind of stuck!

any help would be appreciated
jen x

RE: Relation operation between two arrays

Can you post a trivial numerical example that shows your desired result?  I couldn't quite make it out from your description.

RE: Relation operation between two arrays

(OP)
I think I was confused..have just realised I dont want to be using indexes for what I was working on! Thanks anyway.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources