Replace elements meeting condition with last valid value
1 view (last 30 days)
Show older comments
I have lots of .mat files with a pair of vectors Pn and En, so I wrote a little fraction of it:
n=2;
S=[75 100];
P1=[22.38 29.84 44.76 59.78 75];
E1=[0.795 0.805 0.815 0.826 0.835];
P2=[45.90 68.86 91.81 114.76 130];
E2=[0.78 0.79 0.795 0.805 0.821];
for ef1=1:1:n
PTEfx=eval(sprintf('P%d',ef1));
ETEfx=eval(sprintf('E%d',ef1));
PTmax=max(eval(sprintf('P%d',ef1)));
ETmax=max(eval(sprintf('E%d',ef1)));
if PTmax>S(ef1)
for ef2=1:1:length(PTEfx)
ind(ef2)=PTEfx(ef2)>Sn(ef1);
PTEfx(ind)=[];
ETEfx(ind)=[];
end
end
PTEf(ef1,:)=PTEfx;
ETEf(ef1,:)=ETEfx;
end
I'm trying to create a matrix containing all those vectors, but I want to replace every element from Pn that's bigger than its corresponding value of S to the last value that is less or equal to it. In addition to this, vector En must have the same behavior. So, for this example the result should be:
PTEf=[22.38 29.84 44.76 59.78 75;
45.90 68.86 91.81 91.81 91.81]
ETEf=[0.795 0.805 0.815 0.826 0.835;
0.78 0.79 0.795 0.795 0.795];
I think I'm almost there, but I was only able to delete those bigger values and I don't want to fill the vector with 0's in order to fit in the matrix.
Suggestions?
0 Comments
Accepted Answer
Elias Gule
on 9 May 2016
I guess I understand what you want to do. Try this
PTEf = [P1;P2];
ETEf = [E1;E2];
ij = PTEf>S(1) | PTEf > S(end);
PTEf(ij) = min(PTEf(ij));
ETEf(ij) = min(ETEf(ij));
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!