# Replace a value in a vector with another

12 views (last 30 days)
Rose on 3 Dec 2019
Commented: Rose on 3 Dec 2019
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

Guillaume on 3 Dec 2019
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).

#### 1 Comment

Rose on 3 Dec 2019
quite honestly, as I have tried so many different thing I am kind of lost too in what I was trying to do there. But your solution works perfectly! Thank you so much!