フィルターのクリア

How to keep false as false, and not as zero

13 ビュー (過去 30 日間)
George Smith
George Smith 2022 年 11 月 7 日
コメント済み: George Smith 2022 年 11 月 30 日
I want a vector that is laid out like below:
V=[false,false,false,false,x] ;
When the cell is not "false", I want it to take the value that it is (in this case x) and do something with it, but don't do anything if false. My problem comes from the fact that x can be 0, and that MatLab changes the values "false" to be "0". I can't have 0 instead of false, it has to be distinct.
The workaround currently is a data structure like follows:
V.element=[0,0,0,0,1];
V.value=[0,0,0,0,0];
Therefore, it sees if V.element is 1, and if so, checks V.value, otherwise it skips. This is fine, but just awkward to have to change twice as many values when changing the inputs of the function.
Tl;dr : MatLab makes false into a 0. Stop it MatLab; bad MatLab!

採用された回答

David Hill
David Hill 2022 年 11 月 7 日
If you are just using false as a flag, change false into some other number flag. You could use a particular number that will never be used or nan or inf.
V=[nan,nan,nan,nan,0,5];
V(~isnan(V))*5%do something
ans = 1×2
0 25
  1 件のコメント
George Smith
George Smith 2022 年 11 月 10 日
Fantastic, thank you so much!

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2022 年 11 月 7 日
All the data in the array V must be the same data type. If x is not a logical array, the logical data will be converted to the class of x as shown in the table on this documentation page. This has been the behavior of MATLAB probably since support for logical data was added.
You could have V convert x into logical data, but you probably wouldn't want that either.
x = 2;
V = [false false false false logical(x)]
V = 1×5 logical array
0 0 0 0 1
What exactly is your use case where you're trying to be able to distinguish false and 0 in the same array?
  5 件のコメント
George Smith
George Smith 2022 年 11 月 10 日
Good to know, thank you very much!
George Smith
George Smith 2022 年 11 月 30 日
Sorry, I missed the question
It's for Dirichlet Boundary Conditions
If there is one, it can equal 0
I wanted a simple way of saying "There is one and it is equal to zero" in a single cell rather than two cells saying each bit of information separately

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by