Insert rows from one matrix into another

1 回表示 (過去 30 日間)
Jeff Szkodzinski
Jeff Szkodzinski 2015 年 4 月 6 日
コメント済み: Mohammad Abouali 2015 年 4 月 7 日
I have two matrices that have the same number of columns, something similar to this. Matrix A has 57 rows and B has 9.
A = 2373 259 18.10 23.80 0.20
2500 272 17.00 23.40 -0.10
3000 273 29.20 20.50 -1.30
...
...
B = 2593 273 21.00 22.00 -0.70
3400 280 26.69 24.77 0.50
...
...
I want to test whether the value in the first column of B (2593) is between two values in the first column of A (2500 & 3000). I want to perform this test by looping though each row in B and comparing to the analogous values in A. Then I want to insert each row of matrix B between the appropriate rows in A so that the numbers in the first column are in ascending order...
A = 2373 259 18.10 23.80 0.20
2500 272 17.00 23.40 -0.10
2593 273 21.00 22.00 -0.70
3000 273 29.20 20.50 -1.30
3400 280 26.69 24.77 0.50
...
I tried this, but it said the index exceeds matrix dimensions. Not sure how else to proceed.
for i=1:numel(B)(1:end,1)
for j=1:numel(A)(1:end,1)
if (B(i,1) < A(j,1) && B(i,1) > A(j+1,1))
vertcat(A, B(i,:), A(j,:))
end
end
end

採用された回答

Mohammad Abouali
Mohammad Abouali 2015 年 4 月 7 日
編集済み: Mohammad Abouali 2015 年 4 月 7 日
use sortrows() command as follows:
A = [2373 259 18.10 23.80 0.20; ...
2500 272 17.00 23.40 -0.10; ...
3000 273 29.20 20.50 -1.30];
B = [2593 273 21.00 22.00 -0.70; ...
3400 280 26.69 24.77 0.50];
% merging the two data sets as you asked.
ABmerged=sortrows([A;B],1);
ABmerged
2373.00 259.00 18.10 23.80 0.20
2500.00 272.00 17.00 23.40 -0.10
2593.00 273.00 21.00 22.00 -0.70
3000.00 273.00 29.20 20.50 -1.30
3400.00 280.00 26.69 24.77 0.50
  2 件のコメント
Jeff Szkodzinski
Jeff Szkodzinski 2015 年 4 月 7 日
The solution was much simpler than I was making it. Thanks so much for the help!
Jeff
Mohammad Abouali
Mohammad Abouali 2015 年 4 月 7 日
You are welcome.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by