Getting rid of loops

조회 수: 1 (최근 30일)
Wesso
Wesso 2021년 1월 9일
댓글: Wesso 2021년 1월 10일
Hi, I am trying to clean matrix A (which is a 1064 x 4 matrix) while relying in the cleaning process on Matrices B and C that have the same size as A (Mat file attached). I developed a loop to clean A but I feel that a loop is unnecessary and that I can do it without relying on loops. Could the cleaning process be done much more efficiently? Your help is appreciated.
for i=1:size(A,1)
for j=1:size(A,2)
if A(i,j)<1 && A(i,j)>0 % this is the first issue in the cleaning process. A values between 0 and 1 should be equal to the respective value in matrix X
A (i,j)=B(i,j);
end
if A(i,j)<C(i,j) % this is the 2nd issue I am facing A values lower than their respective C values are erroneous and should be replaced by NaN
A (i,j)=nan;
end
if B(i,j)>100 %this is the third issue: When B is greater than 100, A values should be equal to B values
A (i,j)=B(i,j);
elseif B(i,j)<0 %This is the fourth issue: negative percentages are missing observations
A(i,j)=-999;
end
if C(i,j)==0%This is the 5th issue: when C is zero, A should be zero as well
A(i,j)=0;
end
end
end

채택된 답변

Stephen23
Stephen23 2021년 1월 9일
편집: Stephen23 2021년 1월 9일
Logical indexing is much simpler than using loops:
idx = A<1 & A>0;
A(idx) = B(idx);
A(A<C) = NaN;
idy = B>100 | B<0;
A(idy) = B(idy);
A(C==0) = 0
  댓글 수: 4
Stephen23
Stephen23 2021년 1월 9일
편집: Stephen23 2021년 1월 9일
"A(2,1)=0 and C(2,1)=30. The codes replaced A(2,1) with NaN"
Because that is what you requested. Your original code includes this comment: "A values lower than their respective C values are erroneous and should be replaced by NaN". The value 0 is clearly less than 30, and so that erroneous value is set to NaN. Just as you wrote. If you actually want something else to occur (different to what you specified in your question), then please explain the new rule so that we can help you implement it.
Wesso
Wesso 2021년 1월 10일
my bad I was looking at the wrong matrix. Thanks a lot

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

추가 답변 (1개)

Image Analyst
Image Analyst 2021년 1월 9일
편집: Image Analyst 2021년 1월 9일
Did you consider just using a mask and no for loops?
mask = A<1 & A>0; % this is the first issue in the cleaning process. A values between 0 and 1 should be equal to the respective value in matrix X
A(mask) = B(mask);
mask = A<C; % this is the 2nd issue I am facing A values lower than their respective C values are erroneous and should be replaced by NaN
A(mask) = nan;
mask = B>100; %this is the third issue: When B is greater than 100, A values should be equal to B values
A(mask) = B(mask);
mask = B<0; %This is the fourth issue: negative percentages are missing observations
A(mask) = -999;
mask = C==0%This is the 5th issue: when C is zero, A should be zero as well
A(mask) = 0;
  댓글 수: 7
Image Analyst
Image Analyst 2021년 1월 9일
"Look at A(1,3) it is 2. C(1,3) is 1. When A<C then A should be NaN" <== Yes, so A is not less than 1 there so NaN is not assigned there, as requested. Are you implying that it SHOULD be NaN even though 2 not less than 1????
"A(2,1)=0 and C(2,1)=30. The codes replaced A(2,1) with NaN". Yes, it does get replaced by NaN just like you asked for when A is less than C. Why is that not working well?
Finally, you may have competing conditions. It's possible that some condition may get set to one values, then later in the code you have a different mask and it gets replaced by a different value.
Wesso
Wesso 2021년 1월 10일
my bad I was looking at the wrong matrix. Thanks a lot

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by