Convert for loop to matrix operation

조회 수: 2 (최근 30일)
Bernhard Petri
Bernhard Petri 2023년 3월 29일
답변: daniel mitchell 2023년 3월 29일
I have loaded an image, created a histogram for it and would like to convert this for loop to matrix operation, how would I do that?
h1 = histogram(img1, edges, 'normalization','probability');
cdf = zeros(1, h1.NumBins);
sum_value = 0;
for i = 1:h1.NumBins
sum_value = sum_value + h1.Values(i);
cdf(i) = (h1.NumBins-1)*sum_value;
end
  댓글 수: 1
Antoni Garcia-Herreros
Antoni Garcia-Herreros 2023년 3월 29일
Hello,
You can try using cumsum
sum_value=sum(h1.Values); %This will be always 1 if you use the normalitzation probability in your histogram
cdf=cumsum(h1.Values).*(h1.NumBins-1);

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

답변 (1개)

daniel mitchell
daniel mitchell 2023년 3월 29일
perhaps this is the solution you are looking for?
h1 = histogram(randn(1e7,1), 'normalization','cdf','DisplayStyle','stairs');
cdf = h1.Values;

카테고리

Help CenterFile Exchange에서 Problem-Based Nonlinear Optimization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by