フィルターのクリア

trying to use logical matrix to get remove some elements from matrix instead of getting a same matrix im getting a column matrix

1 回表示 (過去 30 日間)
a=[1,2,3;12,21,2;2,1,2]
b=(a>1)
c=a(b)
im getting result
b =
0 1 1
1 1 1
1 0 1
c =
12
2
2
21
3
2
2
>> i want c matrix to be 3*3 matrix but im getting it as column matrix

採用された回答

Image Analyst
Image Analyst 2020 年 6 月 25 日
Try this:
a=[1,2,3;12,21,2;2,1,2]
c = a .* double(a > 1)

その他の回答 (1 件)

the cyclist
the cyclist 2020 年 6 月 25 日
編集済み: the cyclist 2020 年 6 月 25 日
Assuming you want zeros in the other locations:
a=[1,2,3;12,21,2;2,1,2]
b=(a>1)
c = zeros(size(a));
c(b)=a(b)
c =
0 2 3
12 21 2
2 0 2
  3 件のコメント
the cyclist
the cyclist 2020 年 6 月 25 日
I would think about it this way. The statement
a(a>1)
translates to "Give me the values of a for which a is greater than 1."
There are 7 such elements. MATLAB cannot "know" that you want those 7 elements arranged as they were before, and more critically, it cannot know that you want the "missing" two elements to be zero. It may be obvious to you that that is what is desired, but MATLAB cannot know that. Instead, maybe you (or someone else) actually wanted
c =
NaN 2 3
12 21 2
2 NaN 2
So, MATLAB is coded to return just the 7 elements requested, without making an assumption.
If, instead, you wanted a "mask" on your array that retains the shape, then you can instead code it the way I did, or Image Analyst did.
AJAY CHANDRA DORAGARI
AJAY CHANDRA DORAGARI 2020 年 6 月 25 日
編集済み: AJAY CHANDRA DORAGARI 2020 年 6 月 25 日
thank you for explaining it clearly
thanks alot !!
the cyclist

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

カテゴリ

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