필터 지우기
필터 지우기

Exclude the NaN, 0, empty and Inf values ​​from the analysis.

조회 수: 126 (최근 30일)
Luccas S.
Luccas S. 2022년 2월 11일
댓글: Luccas S. 2022년 2월 11일
When calculating the PE value, I would like it not to calculate when Ia_future = 0, Nan or Inf.
I believe the way I did it, it's still calculating. Because some PE values ​​are still Inf.
Or if there was some way to exclude those values. The problem is that I need to plot a (t,PE) graphic and if I exclude some PE values ​​the two will have different dimensions and I will not be able to analyze the graph..
for n = 4:size(t,1)
X = [Ia(n-1,1) Ia(n-2,1) ; Ia(n-2,1) Ia(n-3,1)];
future = [Ia(n,1) ; Ia(n-1,1)];
C = X\future;
Ia_future(n,1) = C(1,1)*Ia(n,1)+C(2,1)*Ia(n-1,1);
if (isnan(Ia_future(n, 1)) || isinf(Ia_future(n,1) || isempty(Ia_future(n,1) || Ia_future(n,1)==0))) %|| %(isnan(p(n, 1)) || p(n, 1) == 0)
continue
end
PE(n,1)=(Ia(n,1)+Ia_future(n,1))/(2000/5);
end

채택된 답변

Benjamin Thompson
Benjamin Thompson 2022년 2월 11일
Vectorize your calculations using index vectors. For example:
>> Test = [0 1 inf NaN]
Test =
0 1 Inf NaN
>> Inan = isnan(Test)
Inan =
1×4 logical array
0 0 0 1
>> Iinf = isinf(Test)
Iinf =
1×4 logical array
0 0 1 0
>> Igood = ~isinf(Test) & ~isnan(Test)
Igood =
1×4 logical array
1 1 0 0
Then you can calculate PE as a function of Ia_future outside the for loop, something like:
PE(Igood,1)=(Ia(Igood,1)+Ia_future(Igood,1))/(2000/5);
Only the rows of PE corresponding to where Igood is one will be updated. You may need to calculate the index vector looking at both Ia and Ia_future if they can have bad values in different spots.
  댓글 수: 2
Luccas S.
Luccas S. 2022년 2월 11일
편집: Luccas S. 2022년 2월 11일
Cool, I tested it here and it worked, you can do several things with this logic, I think.
If I wanted to ignore the values of PE>1 and PE<0 would it be possible to do something like that too?
Thanks!
Benjamin Thompson
Benjamin Thompson 2022년 2월 11일
Yes you can define an index vector using any kind of comparison test.

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

추가 답변 (1개)

Image Analyst
Image Analyst 2022년 2월 11일
Is this helpful:
data = [0, 9, inf, NaN, 42];
mask = (data ~= 0) & isfinite(data)
mask = 1×5 logical array
0 1 0 0 1
extractedData = data(mask)
extractedData = 1×2
9 42
Using isfinite() takes the place/combines both ~isinf() and ~isnan() all into one simple function.
  댓글 수: 1
Luccas S.
Luccas S. 2022년 2월 11일
Okay, I didn't know about this isfinite function. Thanks too !

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

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by