Assign this or that in MATLAB

Find this useful? Support us: Star on GitHub 6
Category: Other | Language: MATLAB

In MATLAB, "this or that" can be assigned based on certain conditions using logical operators.

For example, suppose you want to assign a value of 1 if a certain variable (let's call it "x") is greater than 5 and a value of 0 if it is less than or equal to 5. You can use the following syntax:

if x > 5
    this_or_that = 1;
else
    this_or_that = 0;
end

Alternatively, you could use the ternary operator in MATLAB to achieve the same thing in a more concise way:

this_or_that = (x > 5) ? 1 : 0;

This assigns a value of 1 to this_or_that if the condition (x > 5) is true, and a value of 0 if it is false.

Other logical operators that can be used for "this or that" assignments in MATLAB include:

- && (logical AND) and || (logical OR)
- & (element-wise AND) and | (element-wise OR)

For example, suppose you have two variables, a and b, and you want to assign a value of "high" if both are greater than 10, a value of "medium" if either one of them is greater than 5, and a value of "low" otherwise. You can use the following code:

if (a > 10) && (b > 10)
    this_or_that = "high";
elseif (a > 5) || (b > 5)
    this_or_that = "medium";
else
    this_or_that = "low";
end

Alternatively, you could use the element-wise operators & and | to achieve the same thing with arrays of values:

a = [9, 12, 7, 15];
b = [11, 13, 4, 8];
this_or_that = strings(size(a));

this_or_that((a > 10) & (b > 10)) = "high";
this_or_that((a > 5) | (b > 5)) = "medium";
this_or_that(~((a > 10) & (b > 10)) & ~((a > 5) | (b > 5))) = "low";

This assigns the appropriate value to each element of the this_or_that array based on the conditions specified.