Add column to a matrix
显示 更早的评论
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 个评论
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
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 个评论
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'
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?
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
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))];
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)
