Compare values and create new vector

4 visualizaciones (últimos 30 días)
Rene
Rene el 30 de Ag. de 2020
Comentada: Rene el 30 de Ag. de 2020
Hello guys
I have two matrices
A:
1 162.880000000000
2 270.270000000000
3 341.660000000000
4 435.050000000000
5 535.590000000000
6 559.990000000000
7 625.190000000000
8 674.740000000000
9 762.620000000000
10 949.380000000000
11 1137
12 1361.90000000000
13 1460.10000000000
14 1517
15 1631.30000000000
16 1746.70000000000
17 1770.60000000000
18 1841.70000000000
19 1843.50000000000
20 1905.10000000000
and B:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
19
17
18
19
20
Now I want a new vector that has the values of the second row of A in the order like so: (the first row is just there for understanding how to sort the vector)
%B Matching value from second row of A
1 162.880000000000
2 270.270000000000
3 341.660000000000
4 435.050000000000
5 535.590000000000
6 559.990000000000
7 625.190000000000
8 674.740000000000
9 762.620000000000
10 949.380000000000
11 1137
12 1361.90000000000
13 1460.10000000000
14 1517
15 1631.30000000000
16 1746.70000000000
19 1843.50000000000
17 1770.60000000000
18 1841.70000000000
19 1843.50000000000
20 1905.10000000000
So I want to compare the first rows of A and B and if the number matches, put the value of the second row of A in.
I already tried this, but this will stop as soon as the value is unequal (so after 16 here).
for i=1:size(A,1)
if B(i,1) == A(i,1)
new(k,1) = A(i,2);
k = k+1;
end
end
How can I say matlab to go on and compare further?
Thanks in advance

Respuesta aceptada

KSSV
KSSV el 30 de Ag. de 2020
It seems you want interp1. Let A be your m*2 matrix and B be n*1.
iwant = interp1(A(:,1),A(:,2),B)
  1 comentario
Rene
Rene el 30 de Ag. de 2020
Thank you very much. This worked perfectly.
It's so easy when you know the right commands.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing 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