Replace elements meeting condition with last valid value
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Elias Gule
2016-5-9
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));
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Particle & Nuclear Physics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!