MATLAB-Truthiness

版本 1.0.2 (78.3 KB) 作者: Yuya Tanaka
Enhancing MATLAB's if-statement.
3.0 次下载
更新时间 2022/11/15

MATLAB-Truthiness

View MATLAB-Truthiness on File Exchange

MATLAB-Truthiness extends the if-statement in MATLAB®.

op self input output
and Falsy(x) (ignored) Falsy(x)
and Truthy(x) Falsy(y) Falsy(y)
and Truthy(x) Truthy(y) Truthy(y)
or Falsy(x) Falsy(y) Falsy(y)
or Falsy(x) Truthy(y) Truthy(y)
or Truthy(x) (ignored) Truthy(x)
xor Falsy(x) Falsy(y) False
xor Falsy(x) Truthy(y) True
xor Truthy(x) Falsy(y) True
xor Truthy(x) Truthy(y) False

📝 Compare to: https://doc.rust-lang.org/std/option/#boolean-operators

Usage Examples

This section describes several exaples for the usage of the add-on MATLAB-Truthiness.

Most of the functionarities of the truthy function can be confirmed by running each of the following examples as a MATLAB script like the following image.

How to run a usage example

The MATLAB's if-statement accepts a N-dimensional array that is evaluatable as either true or false. In contrast, the short-circuit evaluation in MATLAB such as && and || only accepts a scalar true or false.

arr1 = ones(3, 3);
arr2 = ones(5, 5);

if arr1
    if arr2
        disp("All the values of `arr1` and all the values of `arr2` is true");
    end 
end

%% An error happens

if arr1 && arr2
    % NOTE: Cannot reach.
    disp("All the values of `arr1` and all the values of `arr2` is true");
end

%% No error happens

if truthy(arr1) && truthy(arr2)
    % NOTE: Can reach.
    disp("All the values of `arr1` and all the values of `arr2` is true");
end

The class Truthiness supports the operation "and", "or" and "xor".

%% Operation `AND`
assert((truthy(true)  & true ) == true);
assert((truthy(true)  & false) == false);
assert((truthy(false) & true ) == false);
assert((truthy(false) & false) == false);

%% Operation `OR`
assert((truthy(true)  | true ) == true);
assert((truthy(true)  | false) == true);
assert((truthy(false) | true ) == true);
assert((truthy(false) | false) == false);

%% Operation `XOR`
assert((truthy(true) .xor(true) ) == false);
assert((truthy(true) .xor(false)) == true);
assert((truthy(false).xor(true) ) == true);
assert((truthy(false).xor(false)) == false);

The class Truthiness supports lazy evaluation.

%% Lazy Evaluation
% NOTE: Run the below lines as a code section. 

startSecs = tic;
[~] = truthy(true).orElse(@waitForOneSec);
toc(startSecs);
disp('It SHOULD NOT take 1 sec.');

function retval = waitForOneSec(~)
    pause(1);
    retval = true;
end

You can take a look of more examples at truthinessTest.

Installation

With git (and GitHub)::

  1. Open the MATLAB directory or the directory you want to store "MATLAB-truthiness".

  2. Run the command below:

    git clone git@github.com:botamochi0x12/MATLAB-Truthiness
  3. In MATLAB command pallet, run addpath MATLAB-Truthiness.

With MATLAB exchange::

  1. Access the page in MATLAB File Exchange.
  2. Click "Download".

With Add-on Directory in MATLAB console::

:TODO: To be written.

Bug Report and Contribution

Anyone accepting CODE_OF_CONDUCT can contribute to this project, or anyone who found any bugs can submit a report about them to this project.

Feel free to submit your "pull requests" 🔀 or your "issues" 💬.

LICENSE

See LICENSE.

引用格式

Yuya Tanaka (2024). MATLAB-Truthiness (https://github.com/botamochi0x12/MATLAB-Truthiness/releases/tag/v1.0.2), GitHub. 检索时间: .

MATLAB 版本兼容性
创建方式 R2019b
与 R2016b 及更高版本兼容
平台兼容性
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!
版本 已发布 发行说明
1.0.2

See release notes for this release on GitHub: https://github.com/botamochi0x12/MATLAB-Truthiness/releases/tag/v1.0.2

1.0.1

See release notes for this release on GitHub: https://github.com/botamochi0x12/MATLAB-Truthiness/releases/tag/v1.0.1

1.0.0

要查看或报告此来自 GitHub 的附加功能中的问题,请访问其 GitHub 仓库
要查看或报告此来自 GitHub 的附加功能中的问题,请访问其 GitHub 仓库