Replace a value in a vector with another
3 次查看(过去 30 天)
显示 更早的评论
Hey there,
I have this problem I can't seem to figure out.
There are two vectors:
Failures : which includes all the times at which a failure has occurred (42x1)
Detoriation: which is a 4380x2 vector, the first column consisting of hours from 1 till 4380. The second column being the percentage of detoriation at that time.
Combining the hours of the failures with the detoriation vectors allows to find the percentage of detoriation at time of the failure.
However, in some data set the time of the failure is set incorrectly resulting in a level of detoriation of 0 (just after the failure), so I want to change the time of the failure to -1 (the value before it) to take the actual level of detoriation at time of the failure. I have been trying the code below, but can't get it to work.
Any ideas on how to fix this?
Failuretimes=floor(Failures); %round failures to integer to match the measurements of detoriation
failuretimesdet=Detoriation(Failuretimes,2); %find detoriation at the times of failure
for k=1:length(Failuretimes)
idx=(true,Failuretimesdet(k)==0) %create a boolean searching for 0 in the detoriation values matched to the failure times
time_of_zero=Failures(idx) %find the failure time at which detoriation is 0
Failuretimes(idx)=time_of_zero-1 %replace the time of failure with the time of zero -1
failuretimesdet=Detoriation(Failuretimes,2); %find the correct level of detoriation
end
0 个评论
采纳的回答
Guillaume
2019-12-3
idx=(true,Failuretimesdet(k)==0)
is not valid matlab syntax, I'm not even sure what you're trying to do with this.
You seem to be treating logical vectors as a vector of indices. Subtracting 1 from a logical vector makes no sense, subtracting one from indices does. The equivalent of subtracting one from a vector of indices would be shifting the logical vector left by one element.
Anyway, if I understood correctly, this should work:
Failuretimes=floor(Failures); %round failures to integer to match the measurements of detoriation
failuretimesdet=Detoriation(Failuretimes,2); %find detoriation at the times of failure
isnullfailure = Failuretimesdet == 0; %identify which elements of failuretimesdet are 0
failuretimesdet(isnullfailure) = Detoriation(Failuretimes(isnullfailure) - 1, 2); %and subtract 1 from the FAILURETIMES to replace the failuretimesdet.
as long as a null failure doesn't occur at Detoriation(1, 1) of course (since there's no preceding row).
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!