필터 지우기
필터 지우기

Forming loop to perform same action

조회 수: 1 (최근 30일)
SUBHAJIT KAR
SUBHAJIT KAR 2020년 4월 4일
댓글: SHAIK 2024년 2월 26일
I am having problem creating the loop for the task.
Thres = 0.17;
istrue1= ((x>=928 & x<=1139) & y>=Thres)|((x>=2527 & x<=2856) & y>=Thres)|((x>=4113 & x<=4376) & y>=Thres)|((x>=5464 & x<=5643) & y>=Thres)|((x>=7254 & x<=7604) & y>=Thres);
TP = sum(istrue1);
Using the command I can find out the value of TP for a particular value of thres. But I want to vary the value of thres from 0:0.1:1 and want to get 10 values of TP simultaneously rather than changing Thres each time. I know it is easy but not for me. Please help.

채택된 답변

dpb
dpb 2020년 4월 4일
Thres = 0.17;
istrue1=((x>= 928 & x<=1139) & y>=Thres) | ...
((x>=2527 & x<=2856) & y>=Thres) | ...
((x>=4113 & x<=4376) & y>=Thres) | ...
((x>=5464 & x<=5643) & y>=Thres) | ...
((x>=7254 & x<=7604) & y>=Thres);
is same as
istrue1=(x>= 928 & x<=1139 | ...
x>=2527 & x<=2856 | ...
x>=4113 & x<=4376 | ...
x>=5464 & x<=5643 | ...
x>=7254 & x<=7604) & y>=Thres);
Is place for my "syntactic sugar" helper utility iswithin...
istrue1=(iswithin(x, 928,1139) | ...
iswithin(x,2527,2856) | ...
iswithin(x,4113,4376) | ...
iswithin(x,5464,5643) | ...
iswithin(x,7254,7604)) & y>=Thres);
to reduce the clutter of all the tests conditions by moving into the function.
function flg=iswithin(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithin(x,lo,hi)
% returns T for x between lo and hi values, inclusive
flg= (x>=lo) & (x<=hi);
end
  댓글 수: 2
SUBHAJIT KAR
SUBHAJIT KAR 2020년 4월 5일
In this case How to input various ranges for x? The function will take only one lo and hi.
dpb
dpb 2020년 4월 5일
As shown, it doesn't replace the individual tests, just hides the clutter.
I don't see any brilliant vectorizing available here; could build a function that looped over an array of lo, hi intervals or write the loop construct at this level would probably be my choice if there are variable numbers of regions and do this much. It's a situation haven't run into in my own code so hadn't attacked it, specifically.

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

추가 답변 (1개)

Image Analyst
Image Analyst 2020년 4월 4일
편집: Image Analyst 2020년 4월 4일
Rather than be so complicated, break it into parts, like
term1 = (x>=928 & x<=1139)
term2 = y>=Thres
term3 = (x>=2527 & x<=2856)
and so on. Then look at the size and values of each term. This is just basic debugging... Then
put Thresh into a loop
Thres = [0 : 0.1 : 1];
for k = 1 : length(Thres)
thisThres = Thres(k);
istrue1 = ...... function thisThresh instead of Thres
end
  댓글 수: 4
SUBHAJIT KAR
SUBHAJIT KAR 2020년 4월 5일
Many Thanks. It is working Perfectly. I have modified the code as :
Thres = [0 : 0.1 : 1];
z = zeros(1,length(Thres));
for k = 1 : length(Thres)
thisThres = Thres(k);
istrue1 = ((x>=928 & x<=1139) & y>=thisThres)|((x>=2527 & x<=2856) & y>=thisThres)|((x>=4113 & x<=4376) & y>=thisThres)|((x>=5464 & x<=5643) & y>=thisThres)|((x>=7254 & x<=7604) & y>=thisThres);
TP = sum(istrue1);
z(k) = TP;
end
SHAIK
SHAIK 2024년 2월 26일
from the above code what is y.

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

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by