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
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.
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
.
With git (and GitHub)::
-
Open the MATLAB directory or the directory you want to store "MATLAB-truthiness".
-
Run the command below:
git clone git@github.com:botamochi0x12/MATLAB-Truthiness
-
In MATLAB command pallet, run
addpath MATLAB-Truthiness
.
With MATLAB exchange::
- Access the page in MATLAB File Exchange.
- Click "Download".
With Add-on Directory in MATLAB console::
:TODO: To be written.
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" 💬.
See LICENSE.
引用格式
Yuya Tanaka (2024). MATLAB-Truthiness (https://github.com/botamochi0x12/MATLAB-Truthiness/releases/tag/v1.0.2), GitHub. 检索时间: .
MATLAB 版本兼容性
平台兼容性
Windows macOS Linux标签
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!@Falsiness
@LazyLogical
@Logicalness
@Truthiness
版本 | 已发布 | 发行说明 | |
---|---|---|---|
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 |