Insert rows from one matrix into another
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Jeff Szkodzinski
le 6 Avr 2015
Commenté : Mohammad Abouali
le 7 Avr 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 commentaires
Réponse acceptée
Mohammad Abouali
le 7 Avr 2015
Modifié(e) : Mohammad Abouali
le 7 Avr 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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!