I have a matrix A
of size say 5x7
.
If I want to get a logical matrix out of A
based of the elements that satisfy certain conditions, I can do this:
A = [10 47 16 33 47 11 10
19 39 26 19 44 16 12
32 25 26 41 28 24 9
40 22 41 27 32 12 12
5 23 40 18 30 43 22]
>> A > 25 | A < 10
ans =
0 1 0 1 1 0 0
0 1 1 0 1 0 0
1 0 1 1 1 0 1
1 0 1 1 1 0 0
1 0 1 0 1 1 0
Problem: If I want to specify conditions for the row and column indices, what is the best way to do it?
I have a condition like i
(row sub) should be less than j
(column sub).
I could do this using triu
but is there any simple way using logical indexing?
Expected output
0 1 1 1 1 1 1
0 0 1 1 1 1 1
0 0 0 1 1 1 1
0 0 0 0 1 1 1
0 0 0 0 0 1 1
Another example:
Size: 5x3
Condition: (i > j+1) | i == 1 | i == 2
Expected Output:
1 1 1
1 1 1
1 0 0
1 1 0
1 1 1