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.

댓글 수: 1

Mario Martos
Mario Martos 2016년 5월 21일
Hello, first thanks . What I 'm looking for is that when comparing the elements of the two vectors , for example when a (2) = 1.0000 is between the interval b(1) = 0 and b (2) = 2.4 in that row add a column value of 1 as it is between that range. That is, if the condition is met , for example a(2) is greater than or equal b(1) and less than b(2) corresponds to the first interval 1 comparing all elements of the row of the two matrices fulfilled those conditions

댓글을 달려면 로그인하십시오.

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2016년 5월 21일
편집: Andrei Bobrov 2016년 5월 21일

1 개 추천

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)]

댓글 수: 8

Mario Martos
Mario Martos 2016년 5월 21일
Hello, first thanks . What I 'm looking for is that when comparing the elements of the two vectors , for example when a (2) = 1.0000 is between the interval b(1) = 0 and b (2) = 2.4 in that row add a column value of 1 as it is between that range. That is, if the condition is met , for example a(2) is greater than or equal b(1) and less than b(2) corresponds to the first interval 1 comparing all elements of the row of the two matrices fulfilled those conditions
Andrei Bobrov
Andrei Bobrov 2016년 5월 21일
편집: Andrei Bobrov 2016년 5월 21일
see after word 'or'
Mario Martos
Mario Martos 2016년 5월 21일
Hello again, I do not know why when I will run the process with larger matrices (length (b) = 25 and length (a = 684) ) makes me : out = [a, b ( cumSum ( IsMember (a, b ( : , 1) ) ) , 2) ] Index Exceeds matrix dimensions .
Why?
Andrei Bobrov
Andrei Bobrov 2016년 5월 21일
I do not know, I do not see your data
my data would be the nmat matrix (Interested in column 6 are the data shown in column 1 of the Bach matrix.) to which I want to add the column compared with the Bach matrix are as follows :
nmat =
0 0.9979 3.0000 62.0000 75.0000 0 0.5987
0.5000 0.2479 1.0000 78.0000 75.0000 0.3000 0.1487
0.5000 0.2479 2.0000 78.0000 75.0000 0.3000 0.1487
0.7500 0.2479 1.0000 79.0000 75.0000 0.4500 0.1487
0.7500 0.2479 2.0000 79.0000 75.0000 0.4500 0.1487
1.0000 0.4979 1.0000 81.0000 75.0000 0.6000 0.2988
1.0000 0.4979 2.0000 81.0000 75.0000 0.6000 0.2988
1.5000 0.4979 1.0000 72.0000 75.0000 0.9000 0.2988
1.5000 0.4979 2.0000 72.0000 75.0000 0.9000 0.2988
1.5000 0.4979 3.0000 54.0000 75.0000 0.9000 0.2988
2.0000 0.4979 1.0000 71.0000 75.0000 1.2000 0.2988
2.0000 0.4979 2.0000 71.0000 75.0000 1.2000 0.2988
2.0000 0.9979 3.0000 55.0000 75.0000 1.2000 0.5987
2.5000 0.2479 1.0000 76.0000 75.0000 1.5000 0.1487
2.5000 0.2479 2.0000 76.0000 75.0000 1.5000 0.1487
2.7500 0.2479 1.0000 78.0000 75.0000 1.6500 0.1487
2.7500 0.2479 2.0000 78.0000 75.0000 1.6500 0.1487
3.0000 0.4979 1.0000 79.0000 75.0000 1.8000 0.2988
3.0000 0.4979 2.0000 79.0000 75.0000 1.8000 0.2988
3.5000 0.4979 1.0000 71.0000 75.0000 2.1000 0.2988
3.5000 0.4979 2.0000 71.0000 75.0000 2.1000 0.2988
3.5000 0.4979 3.0000 52.0000 75.0000 2.1000 0.2988
4.0000 0.4979 1.0000 69.0000 75.0000 2.4000 0.2988
4.0000 0.9979 3.0000 54.0000 75.0000 2.4000 0.5988
4.5000 0.2479 1.0000 74.0000 75.0000 2.7000 0.1488
4.7500 0.2479 1.0000 76.0000 75.0000 2.8500 0.1488
5.0000 0.4979 1.0000 78.0000 75.0000 3.0000 0.2988
5.5000 0.4979 1.0000 69.0000 75.0000 3.3000 0.2988
5.5000 0.4979 3.0000 50.0000 75.0000 3.3000 0.2988
6.0000 0.2479 1.0000 67.0000 75.0000 3.6000 0.1488
6.0000 0.4979 3.0000 59.0000 75.0000 3.6000 0.2988
6.2500 0.2479 1.0000 78.0000 75.0000 3.7500 0.1488
6.5000 0.2479 1.0000 76.0000 75.0000 3.9000 0.1488
6.5000 0.4979 3.0000 55.0000 75.0000 3.9000 0.2987
6.7500 0.2479 1.0000 74.0000 75.0000 4.0500 0.1487
7.0000 0.2479 1.0000 73.0000 75.0000 4.2000 0.1487
7.0000 0.4979 3.0000 57.0000 75.0000 4.2000 0.2987
7.2500 0.2479 1.0000 83.0000 75.0000 4.3500 0.1487
7.5000 0.2479 1.0000 81.0000 75.0000 4.5000 0.1487
7.5000 0.4979 3.0000 45.0000 75.0000 4.5000 0.2987
7.7500 0.2479 1.0000 79.0000 75.0000 4.6500 0.1487
8.0000 0.2479 1.0000 78.0000 62.0000 4.8000 0.1487
8.0000 0.4979 3.0000 50.0000 75.0000 4.8000 0.2987
8.2500 0.2479 1.0000 76.0000 62.0000 4.9500 0.1487
8.5000 0.2479 1.0000 74.0000 62.0000 5.1000 0.1487
8.5000 0.4979 3.0000 62.0000 75.0000 5.1000 0.2987
8.7500 0.2479 1.0000 76.0000 62.0000 5.2500 0.1487
9.0000 0.2479 1.0000 78.0000 62.0000 5.4000 0.1487
9.0000 0.4979 3.0000 61.0000 75.0000 5.4000 0.2987
9.2500 0.2479 1.0000 76.0000 62.0000 5.5500 0.1487
9.5000 0.2479 1.0000 78.0000 62.0000 5.7000 0.1487
9.5000 0.4979 3.0000 59.0000 75.0000 5.7000 0.2987
9.7500 0.2479 1.0000 80.0000 62.0000 5.8500 0.1487
10.0000 0.2479 3.0000 57.0000 75.0000 6.0000 0.1487
10.0000 0.9979 1.0000 81.0000 62.0000 6.0000 0.5987
10.2500 0.2479 3.0000 56.0000 75.0000 6.1500 0.1487
10.5000 0.2479 2.0000 73.0000 75.0000 6.3000 0.1487
10.5000 0.2479 3.0000 57.0000 75.0000 6.3000 0.1487
10.7500 0.2479 2.0000 74.0000 75.0000 6.4500 0.1487
10.7500 0.2479 3.0000 59.0000 75.0000 6.4500 0.1487
11.0000 0.2479 3.0000 61.0000 75.0000 6.6000 0.1487
11.0000 0.4979 2.0000 76.0000 75.0000 6.6000 0.2987
11.0000 1.9979 1.0000 76.0000 62.0000 6.6000 1.1987
11.2500 0.2479 3.0000 62.0000 75.0000 6.7500 0.1487
11.5000 0.2479 3.0000 61.0000 75.0000 6.9000 0.1487
11.5000 0.4979 2.0000 67.0000 75.0000 6.9000 0.2987
y Bach:
Bach =
0 1.0000
2.4000 2.0000
4.8000 3.0000
Mario Martos
Mario Martos 2016년 5월 22일
Using the second method :
[ ~ , ii ] = histc ( nmat [ Bach ( : , 1) ; inf ] ) ;
out = [ nmat , Bach (ii, 2)]
Error using horzcat Dimensions of arrays being concatenated are not consistent .
Andrei Bobrov
Andrei Bobrov 2016년 5월 22일
편집: Andrei Bobrov 2016년 5월 22일
[~,ii] = histc(nmat(:,6), [Bach(:,1);inf]);
out = [nmat, Bach(ii,2)];
or (R2015a and later)
out = [nmat, discretize(nmat(:,6),[Bach(:,1);inf],Bach(:,2))];
Mario Martos
Mario Martos 2016년 5월 22일
And if I work, many thanks! and apology for the inconvenience

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2016년 5월 21일

댓글:

2016년 5월 22일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by