필터 지우기
필터 지우기

Replace a value in a vector with another

조회 수: 2 (최근 30일)
Rose
Rose 2019년 12월 3일
댓글: Rose 2019년 12월 3일
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
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).
  댓글 수: 1
Rose
Rose 2019년 12월 3일
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!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by