You shouldn't use i
and j
as variables in MATLAB, as they are used to denote the imaginary unit (sqrt(-1)
).
In fact, the official MATLAB documentation warns you about it. It's better to use 1i
or 1j
for the imaginary unit.
The documentation says the performance is better, but that's not really true. The main problem however is that using those variable names
can result in some stange behavior. As i
is not undefined, you don't get the usual Undefined function or variable 'x'.
For instance, assume you intended to write i = true
, but forgot it. Now, try out:
if i == true
disp('What happens here?')
end
This should normally result in "What happens here?" being displayed on the screen, but since i = sqrt(-1)
, which is neither true nor false, you get nothing. You won't get anything for this either:
if i == false
disp('What happens here?')
end
Similarly, this will result in an infinite loop:
while i < 10
i = i * 2;
end
So, change i
and j
to ii
and jj
(this is common for users on SO at least), or some other variable names.
Now, what about the loops and ifs?
You are right, this should definitely be vectorized. Althoug the performance of loops have been drastically improved after the new exectuion engine
was included, vectorization is still fastest in most cases.
First off, you should know that boolean values can be treated as numeric values. They can be subtracted, added, etc. Furthermore, any non-zero real number is considered to be true
, while a zero is considered to be false
. Also, if a == true
is the same as just if a
, and if a == false
is the same as just if ~a
(~
means not in this case), or if a ~= 0
.
If you had only the first part about where result
is defined only be the values of A
, then this could be done quite simply:
results = (A > 0.75) % or `results = (A > 0.75)
The part with A < 0.25
is irrelevant there, since those elements are already zero.
x | y
means x == true
and/or y == true
. So, it's enough that one of the conditions are true. If you do x & y
, then both x
and y
must be true.
Since you include the part with B
, you must add a term, the above line is not enough. First note that the part with B
is only relevant if the two conditions
A < 0.75
and
A > 0.25
are both false. We can disregard the A < 0.75
part, since those elements are already set to 1
.
In plain text: The elements in result
shall be 1 if either A > 0.75
or A > 0.25
and B == 1
. Since the last part has an and in there, we must use &
.
So, the loops and ifs can simply be rewritten as:
result = ((A > 0.75) | (A > 0.25 & B))
It's a matter of preference, but you might want to do ... & B == true
alternatively ... & B == 1
for clarity. It's common to ignore the true
part, but it is a bit clearer to include it.
Example:
A =
0.568800 0.162200 0.165600 0.689200
0.469400 0.794300 0.602000 0.748200
0.011900 0.311200 0.263000 0.450500
0.337100 0.528500 0.654100 0.083800
B =
1 0 1 0
0 0 1 1
0 1 1 0
0 0 1 1
res =
1 0 0 0
0 1 1 1
0 1 1 0
0 0 1 0
I
relevant to the question? \$\endgroup\$ – 200_success Jun 20 '16 at 23:19