Borrar filtros
Borrar filtros

Insert rows from one matrix into another

3 visualizaciones (últimos 30 días)
Jeff Szkodzinski
Jeff Szkodzinski el 6 de Abr. de 2015
Comentada: Mohammad Abouali el 7 de Abr. de 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

Respuesta aceptada

Mohammad Abouali
Mohammad Abouali el 7 de Abr. de 2015
Editada: Mohammad Abouali el 7 de Abr. de 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 comentarios
Jeff Szkodzinski
Jeff Szkodzinski el 7 de Abr. de 2015
The solution was much simpler than I was making it. Thanks so much for the help!
Jeff
Mohammad Abouali
Mohammad Abouali el 7 de Abr. de 2015
You are welcome.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by