While loop not performing
이전 댓글 표시
Hi,
I have the following code. My aim is to make each and every entry of vector a zero, i.e. to make it a vector of zeros. I am doing this by using a while loop and choosing a random number within a's dimensions and making that entry 0. It should do this until all entries become zero and it should, in addition, count the times it took to make the vector zero.
My problem is that the loop doesn't work at all. How can I make it better, or where is the mistake I cannot see here?Hi,
I have the following code. My aim is to make each and every entry of vector a zero, i.e. to make it a vector of zeros. I am doing this by using a while loop and choosing a random number within a's dimensions and making that entry 0. It should do this until all entries become zero and it should, in addition, count the times it took to make the vector zero.
My problem is that the loop doesn't work at all. How can I make it better, or where is the mistake I cannot see here?
a=ones(1,10);
while(a==zeros(1,10))
N=1;
x=randi([1 10])
a(x)=0
N=N+1;
end
a
end
채택된 답변
추가 답변 (1개)
Uendi Hajderaj
2019년 2월 16일
편집: Uendi Hajderaj
2019년 2월 16일
댓글 수: 1
Luigi Mario
2019년 2월 27일
You already know the answer. Just add another array.
a=ones(1,10);
timesItTakes = zeros(1,10);
for M=1:10
while (a(M)~=0);
x=randi([1 10])
a(x)=0
timesItTakes(x) = timesItTakes(x) + 1
end
end
a
카테고리
도움말 센터 및 File Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!