loop for finding a threshold and change corresponding number

hi, i have a dataset in the form [2000x730], where my variable of interest is 730. i want to use the loop and find the following equation.
aa=(data)-min(data)/max(data)+min(data);
which i already did with following code:
[m,n]=size(data);
% aa=zeros(m,n);
for i=1:m;
aa(i,:)=(data(i,:)-min(data(i,:)))/(max(data(i,:))-min(data(i,:)));
end
in second part since i have output file aa[2000x730]. i want to find the number where my data meets the required threshold i-e 0.76,
any idea how to loop for it,
regards and thnx in advance

댓글 수: 1

Whichever version of matlab, you're using the loop in your example is totally unnecessary.
In r2016b:
aa = (data - min(data, [], 2)) ./ (max(data, [], 2) - min(data, [], 2));
Prior to R2016b:
aa = bsxfun(@rdivide, bsxfun(@minus, data, min(data, [], 2)), max(data, [], 2) - min(data, [], 2));

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

 채택된 답변

Guillaume
Guillaume 2017년 1월 9일
As per my comment to the question, the loop you've shown is unnecessary:
aa = (data - min(data, [], 2)) ./ (max(data, [], 2) - min(data, [], 2)); %in R2016b
I'm assuming that you want to find where aa first crosses the threshold for each row. If not, see ImageAnalyst's answer.
[r, c] = find(aa >= 0.76)
crossthreshold = accumarray(r, c, [size(aa, 1), 1], @min)

댓글 수: 1

Thank You sir, your codes are really helpful and saved me a lot of time, bundles of respect for all who helps from this platform.

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

추가 답변 (1개)

Image Analyst
Image Analyst 2017년 1월 8일
Why loop? Why not do it vectorized in a single line:
atOrAboveThreshold = aa >= 0.76;

댓글 수: 5

Sir, i want a loop to see when my data exceeds the threshold, suppose i have one year data and i want the day when the value exceeds the threshold. so the time steps here indicates number of the number of days, which starts from 1 to 73. then from each time series i can see the day when the values exceeds threshold and i can only plot the days in a sequence. Thank you for your response and i am waiting for further suggestion ....
@khan: vectorized code will be faster and simpler than using a loop.
can you help me with the vectorized code, i wrote a loop but since i have 40 years daily twice readings so it is taking too much time to calculate the output. thanks in advance sir
The code I gave you was vectorized. atOrAboveThreshold is a logical vector of 1 or 0 depending on if the element is above or below the threshold. If you want a count of the number above, you can do
numAbove = sum(atOrAboveThreshold);
If you want to extract only the numbers above or below the threshold you can do
aaAbove = aa(atOrAboveThreshold);
aaBelow = aa(~atOrAboveThreshold);
These will have fewer numbers of elements.
If you want to mask the ones below, which will keep the same number of elements but just set the "below" elements to zero, you can do
aa(~atOrAboveThreshold) = 0; % Set below values to 0
You should try to learn about logical indexing. It is one of the most powerful features of MATLAB and the key to writing vectorized code.
thank you sir, your code really changed my way of thinking and writing codes for my research. i hope i can learn more from you. Respect and regards sir.

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2017년 1월 8일

댓글:

2017년 1월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by