% member ship value taken by zeros initilization
U=zeros(2,3);
% **********************************************
% each row is randomly initialize and
% must be 1 [ i.e., summation of complete row value]
% **********************************************
for i=1:2
% for each i is 1 to 2, iteratively increased row
while(1)
total=0;
for j=1:3
U(i,j)=rand();
% generaly random value is less than one
total=total+U(i,j);
% each row, every colomn value is added to variable 'total'
end
if (total == 1)
% check value is equal to one or not,example total=1.1235
% then total considers as 1 but my point is exact total
% varible must be 'one'
break;
end
end
end
disp(U);
loop runs infinite so that please describe how to stop it

5 comentarios

Adam
Adam el 5 de Dic. de 2016
while(1)
defines an infinite loop. It's always dangerous to do if you cannot be 100% certain that within the loop you have a guaranteed termination criterion.
pavan kumar teegala
pavan kumar teegala el 6 de Dic. de 2016
my point, a row array is random generated value with keyword--> rand()
but all row values must be equal to 'one'.
ex:
col 1 col 2 col 3
row 1 : 0.1254 0.6412 0.23334
row 2 : 0.525 0.3500 0.125
row 1 : (0.1254+0.6412+0.23334)=1
row 2 : (0.525+0.3500+0.125)=1
Adam
Adam el 6 de Dic. de 2016
Editada: Adam el 6 de Dic. de 2016
Yes, but it doesn't take much to realise that will pretty much never happen, as Image Analyst points out. What do you think the percentage chance is of 3 totally random numbers of ~15 decimal places summing to exactly 1?, even before you take into account floating point inaccuracies within that sum even if they theoretically did sum to 1.
pavan kumar teegala
pavan kumar teegala el 6 de Dic. de 2016
Thank You @Adam, I tried many times to do theoretically and finally I got practical implementation on the code. just while condition with flag variable assume as true, if inner condition fails then flag variable assume as false.......
function [U]=memv(mrow,k) % memv- fuzzy membership value [0,1] and mrow --> max rows of data % k --> no of clusters U=zeros(mrow,k);
for r=1:mrow % for each i is 1 to 2, iteratively increased row flag=true; while(flag) total=0; for cl=1:k
U(r,cl)=rand();
% generaly random value is less than one
total=total+U(r,cl);
% each row, every colomn value is added to variable 'total'
end
smallNeglate=0.00001;
if abs(total -1) <smallNeglate
% check total value is equal to one or not,example
% total=1.1235 in matlab then total considers as 1
% but my point is exact total varible must be 'one'
flag=false;
end
end
end % disp(U);
Image Analyst
Image Analyst el 7 de Dic. de 2016
I don't see any questions so I assume you have it solved now. However there is the implied question of "How do I format my code so it is readable?" and here is the answer to that question: http://www.mathworks.com/matlabcentral/answers/13205#answer_18099

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 5 de Dic. de 2016
Editada: Image Analyst el 5 de Dic. de 2016

0 votos

Since total is random numbers, it will never equal exactly 1, so see if it exceeds 1 instead
if total >= 1
Of check within a tolerance
if abs(total-1) < someSmallNumber

Más respuestas (2)

Roger Stafford
Roger Stafford el 5 de Dic. de 2016

0 votos

Oops! I've done something wrong and seemingly erased both my answer and Jan Simon's. I don't know how to correct it, but a thousand pardons Jan!

2 comentarios

Image Analyst
Image Analyst el 5 de Dic. de 2016
The way to correct it is to use the Lazarus browser plugin. It's save me numerous times! Highly recommended!
Jan
Jan el 6 de Dic. de 2016
@Roger: Never mind. I have a bunch of answers here in my drawer and simply choose another one. :-)

Iniciar sesión para comentar.

Jan
Jan el 6 de Dic. de 2016

0 votos

You can either normalize the data:
U = rand(2, 3);
U = bsxfun(@rdivide, U, sum(U, 2));
Or use Roger's smart function FEX: random-vectors-with-fixed-sum

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by