Let's do logic operations with vectors and matrices.
Our example uses more common informations.
We have a vector and each element is the height of a person (in meters).
v = [1.55 1.82 1.48 1.71 1.62 1.94 2.00]'
Now, we need to know who is taller than 1.80m.
So, we can do an analysis element by element, like this:
v(1) > 1.80
v(2) > 1.80
v(3) > 1.80
v(4) > 1.80
v(5) > 1.80
v(6) > 1.80
v(7) > 1.80
But, the Scilab has a very simple solution.
v > 1.8 // this operation is non-dependent of vector's dimensionality
Look the script.
-->v = [1.55 1.82 1.48 1.71 1.62 1.94 2.00]'
v =
1.55
1.82
1.48
1.71
1.62
1.94
2.
-->v > 1.8
ans =
F
T
F
F
F
T
T
But, if we want the positions of the elements (people) taller than 1.80m then we can do this:
-->positions = find(v > 1.8)
positions =
2. 6. 7.
The elements in v these are taller than 1.8m are the 2nd (1.82m), 6th (1.94m) and 7th (2.00m).
We can do the same analysis for matrices.
A very useful example is a binary matrix with equal probability of zeros and ones.
Look the script and post to me your comments.
-->x = rand(3, 3)
x =
0.5667211 0.0568928 0.7279222
0.5711639 0.5595937 0.2677766
0.8160110 0.1249340 0.5465335
-->y = x > 0.5
y =
T F T
T T F
T F T
In showed example we have more ones ("Trues"), I ask: why?
3 comments:
a = [ 1 2 3 4 11 20 10 ]
b = a>9
What is the smart way of subtract 10 from each column that has a value over 9? I'm looking for a matrice operation to avoid loops... Is it possible?
Thank you!
Hello Peter, you can try something like this:
a = [ 1 2 3 4 11 20 10 ];
aux_positions = find(a > 9);
b = a(aux_positions);
b = b - 10;
a(aux_positions) = b;
Best regards.
Hi Alex! This is a great solution. Thank you very much!
Post a Comment