Replace elements meeting condition with last valid value
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?
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements 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!