필터 지우기
필터 지우기

How to make sure that results are always positive?

조회 수: 11 (최근 30일)
Anirudh Chandra
Anirudh Chandra 2014년 2월 10일
답변: Stef van Haaren 2019년 10월 8일
Hi, i have written a program which would give me a continuous outputs in the form of a histogram, using a set of for and while loops. I would like to know, how i can ensure that the results of the equations are always positive, ie, all the histogram columns lie in the positive axis? Here is the code i have written. All inputs are welcome and appreciated.
M = 1000.*ones(1,1000);
p = rand;
d = 1000;
n = randi(d);
m = randi(d);
for i = 1:1000
while(M(1,n)>0||M(1,m)>0)
n = randi(d);
m = randi(d);
if n==m % This loop ensures that the values of n and m remain distinct%
while n==m
n = randi(d);
m = randi(d);
if ne(n,m)==1
break;
end
end
end
M(1,n) = M(1,n) - (p.*( M(1,n)+M(1,m)))./2;
M(1,m) = M(1,m) + (p.*( M(1,n)+M(1,m)))./2;
if (M(1,n)<0&&M(1,m)<0)
break;
end
end
end

답변 (1개)

Stef van Haaren
Stef van Haaren 2019년 10월 8일
Use abs()

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by