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 件のコメント
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 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
