MATLAB: Add column to a matrix

#matrix #concatenate

Hello, I wanted to ask about how you might concatenate two vectors or matrices under conditions , ie comparing the elements. For example I have a matrix such that:
a =
0
1.0000
2.0000
2.4000
2.5000
3.0000
3.5000
4.8000
5.0000
5.5000
and another:
b =
0 1.0000
2.4000 2.0000
4.8000 3.0000
And I want to have such a result that :
result =
0 1.0000
1.0000 1.0000
2.0000 1.0000
2.4000 2.0000
2.5000 2.0000
3.0000 2.0000
3.5000 2.0000
4.8000 3.0000
5.0000 3.0000
5.5000 3.0000
That is, comparing with the first column of b assign the value 1, 2,3 .. depending on whether the value is greater or is between two numeros.Por example 2.4 is greater and equal to the second element b but less the third and so …. Thanks in advanced.

Best Answer

a =[ 0
1.0000
2.0000
2.4000
2.5000
3.0000
3.5000
4.8000
5.0000
5.5000];
b =[ 0 1.0000
2.4000 2.0000
4.8000 3.0000];
out = [a, b(cumsum(ismember(a,b(:,1))),2)]
or
[~,ii] = histc(a,[b(:,1);inf]);
out = [a, b(ii,2)]