Add column to a matrix
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
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
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
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
2016년 5월 21일
편집: Andrei Bobrov
2016년 5월 21일
see after word 'or'
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
2016년 5월 21일
I do not know, I do not see your data
Mario Martos
2016년 5월 22일
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
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
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
2016년 5월 22일
And if I work, many thanks! and apology for the inconvenience
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
