フィルターのクリア

replace not selected value by zero

2 ビュー (過去 30 日間)
HASAN AL-KAF
HASAN AL-KAF 2018 年 1 月 28 日
コメント済み: Star Strider 2018 年 1 月 28 日
Hi I have 10 values. my code select 3 values, for example, if x=[1 2 3 4 5 6 7 8 9 10] and my code select [1 3 5] i want a code to do this x= [1 0 3 0 5 0 0 0 0 0]?.

採用された回答

Star Strider
Star Strider 2018 年 1 月 28 日
This works:
x=[1 2 3 4 5 6 7 8 9 10];
ToKeep = [1 3 5];
idx = ismember(x, ToKeep);
Result = x .* idx
Result =
1 0 3 0 5 0 0 0 0 0
  2 件のコメント
Image Analyst
Image Analyst 2018 年 1 月 28 日
Totally different than what I did and that made me see the ambiguity. We need Hasan to clarify if the [1,3,5] are the indexes of x that should be kept (like I assumed), or the values of x that should be kept (like you assumed).
Star Strider
Star Strider 2018 年 1 月 28 日
‘I have 10 values. my code select 3 values,’
I interpret ‘values’ as indicating array elements.

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2018 年 1 月 28 日
Here's one way:
x=[1 2 3 4 5 6 7 8 9 10]
mask = [1 3 5]
x2 = zeros(1, length(x)); % Initialize x2
x2(mask) = x(mask) % Pull over values in the mask from x and put them in the same column.

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by