Insert rows from one matrix into another

1 visualizzazione (ultimi 30 giorni)
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

Risposta accettata

Mohammad Abouali
Mohammad Abouali il 7 Apr 2015
Modificato: Mohammad Abouali il 7 Apr 2015
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 Commenti
Jeff Szkodzinski
Jeff Szkodzinski il 7 Apr 2015
The solution was much simpler than I was making it. Thanks so much for the help!
Jeff

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by