フィルターのクリア

Add column to a matrix

3 ビュー (過去 30 日間)
Mario Martos
Mario Martos 2016 年 5 月 21 日
コメント済み: Mario Martos 2016 年 5 月 22 日
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 日
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 件のコメント
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 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by