Insert rows from one matrix into another
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Jeff Szkodzinski
am 6 Apr. 2015
Kommentiert: Mohammad Abouali
am 7 Apr. 2015
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
0 Kommentare
Akzeptierte Antwort
Mohammad Abouali
am 7 Apr. 2015
Bearbeitet: Mohammad Abouali
am 7 Apr. 2015
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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!