Compare values and create new vector

조회 수: 1 (최근 30일)
Rene
Rene 2020년 8월 30일
댓글: Rene 2020년 8월 30일
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

채택된 답변

KSSV
KSSV 2020년 8월 30일
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
Rene
Rene 2020년 8월 30일
Thank you very much. This worked perfectly.
It's so easy when you know the right commands.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by