Embedding an array into another: vectorization method
显示 更早的评论
I have two arrays. The first one is a consecutive sequential one, like:
seq1 =
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
...continues
The second one is like:
seq2 =
2 250
3 260
5 267
6 270
8 280
10 290
13 300
18 310
20 320
21 330
...continues
I need to embed `seq2` into `seq1` in such a way that I end up with the sequence:
seq3 =
1 0
2 250
3 260
4 260
5 267
6 270
7 270
8 280
9 280
10 290
11 290
... continues
I could do this with loops but the arrays are really big so I don't want to use two `for` loops, it is taking too long. How can I do this in a vectorised manner?
2 个评论
dpb
2017-5-25
What are p, q, r, ... ? Constants, additional vectors, ... ?
采纳的回答
更多回答(1 个)
>> A = [
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0 ];
>> B = [
2 250
3 260
5 267
6 270
8 280
10 290
13 300
18 310
20 320
21 330 ];
>> Z = A;
>> Z(:,2) = interp1(B(:,1),B(:,2),A(:,1),'previous')
>> Z =
1 NA
2 250
3 260
4 260
5 267
6 270
7 270
8 280
9 280
10 290
2 个评论
Andrei Bobrov
2017-5-25
编辑:Andrei Bobrov
2017-5-25
+1
Z = (min([A(:,1);B(:,1)]):max([A(:,1);B(:,1)]))';
Z(:,2) = interp1(B(:,1),B(:,2),Z,'previous');
Tanmay
2017-5-25
类别
在 帮助中心 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!