Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Relation operation between two arrays 1

Status
Not open for further replies.

JenMacR

Electrical
Joined
Aug 10, 2005
Messages
4
Location
NL
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
 
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;

 
% 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.
 
% 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.
 
VisiGoth,

There's no need for the find() call in your code. It just adds a little overhead.
 
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
 
cheers guys.

works a treat.

jenxx
 
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
 
Can you post a trivial numerical example that shows your desired result? I couldn't quite make it out from your description.
 
I think I was confused..have just realised I dont want to be using indexes for what I was working on! Thanks anyway.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top