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

 採用された回答

Walter Roberson
Walter Roberson 2019 年 2 月 15 日

0 投票

When you use if or while with a condition, then MATLAB only considers the condition to be true if all the values being tested are non-zero.
You initialize a to a vector of 1's, and test whether that vector == 0. None of the entries are equal to 0, so not all of the values are true (non-zero), so the while ends immediately.
You could test
while any(a)
which would be equivalent to testing
while any(a ~= 0)

4 件のコメント

Image Analyst
Image Analyst 2019 年 2 月 16 日
If you ever wanted to compare a vector a to an vector of zeros, you'd use isequal():
z = zeros(1, 10);
while isequal(a, z)
%etc.
or you could use all(a == 0)
Walter Roberson
Walter Roberson 2019 年 2 月 16 日
isequal(a,z) has the advantage that first it tests whether the size() of the variables is the same and if the variables are compatible types, and says false() if those are not true. If the size and datatypes are compatible then isequal does the equivalent of all(a(:) == z(:)) . Be warned that nan does not compare equal to nan.
So, if you are sure that a and z are the same size and data type, then isequal(a,z) is the same as (a==z) . But isequal has the advantage that isequal('hello', 'hi') returns false instead of returning an error like you would get for 'hello'=='hi' (because of size differences.)
Uendi Hajderaj
Uendi Hajderaj 2019 年 2 月 16 日
編集済み: Uendi Hajderaj 2019 年 2 月 16 日
I now understand the meaning behind the code and considering the output it makes perfect sense. But I thought the while loop should work in the sense that it should perform the task given to it after the condition, until the condition is satisfied. Since this clearly doesn't work, how is then possible to turn a vector of ones to a vactor of zeros, by counting the number of times it takes for it it become a vector of zeros?
Walter Roberson
Walter Roberson 2019 年 2 月 16 日
N = 10;
a = ones(1, N);
C = 0;
while any(a ~= 0)
C = C + 1;
idx = randi([1 N]);
a(idx) = 0;
end
fprintf('Needed %d iterations\n', C);

サインインしてコメントする。

その他の回答 (1 件)

Uendi Hajderaj
Uendi Hajderaj 2019 年 2 月 16 日
編集済み: Uendi Hajderaj 2019 年 2 月 16 日

0 投票

Ok I found another way how to do this and it works perfectly fine. It does in the end return a vector of zeros after successfully turning all of its entries from 1 to 0.
Now what I'm struggling with, is finding a way to count the times, it took for it to make all the entries zero. Is there any way I can do this, like for example as a sum of all the times one of the entries of vector a has changed?
a=ones(1,10);
for M=1:10
while (a(M)~=0);
x=randi([1 10])
a(x)=0
end
end
a

1 件のコメント

Luigi Mario
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 ExchangeOperating on Diagonal Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by