Info

この質問は閉じられています。 編集または回答するには再度開いてください。

While loop with OR

1 回表示 (過去 30 日間)
Hatim Altarteer
Hatim Altarteer 2020 年 7 月 5 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
i have an array [1:14] and i want to choose 5 numbers from that array excluding the 1,4,5,6,8 indicies.
how can i do that in a while loop?

回答 (2 件)

madhan ravi
madhan ravi 2020 年 7 月 5 日
setdiff(array, [1,4,5,6,8])

Image Analyst
Image Analyst 2020 年 7 月 6 日
Trying to be as general as p[ossible, try this:
% Define initial vector:
v = 1:14
% Get everything except for certain values:
v2 = setdiff(v, [1,4,5,6,8])
% Get up to 5 randomly located indexes of v2.
numElements = min(5, length(v2))
indexes = sort(randperm(length(v2), numElements)) % Sorting is optional
% Get the values at those indexes (will be the same for this simple example, but not in general).
values = v2(indexes)
You'll see
v =
1 2 3 4 5 6 7 8 9 10 11 12 13 14
v2 =
2 3 7 9 10 11 12 13 14
numElements =
5
indexes =
1 2 4 6 7
values =
2 3 9 11 12

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by