Adding new values in between existing values in an array

조회 수: 52 (최근 30일)
Sancheet Hoque
Sancheet Hoque 2017년 1월 19일
댓글: Abubakar Sani-Mohammed 2019년 2월 19일
I have 5 arrays with four values in each of them. Longitude array, Latitude Array, Velocity U array, Velocity V array and a time array in datenum form. This is ocean current data, and I want to simulate a ship moving through four values in 10 second intervals. So its like adding 360 points new datenum values between each datenum in the time array. So since the time array will increase if I did that. I would have to do something similar to the other arrays, but keep them the same length as the time array. That way i will be able to plot them. Since the velocity will not be in ascending order, how can fill the points in between each value. SO basically i want to make new points that coincide with the actual points.
Below I kind of tried to type what I want my code to do.
tarray: 4x1 single
lon: 4x1 double
lat: 4x1 double
u: 4x1 double
v: 4x1 double
after adding 10 seconds in between datenums
tarray: 1440x1 single
lon: 1440x1
lat: 1440x1
u: 1440x1
v: 1440x1
I'm not sure how to do this and I have been looking into it. If someone could help me progress into my study that would be great!
  댓글 수: 2
Jorge Mario Guerra González
Jorge Mario Guerra González 2017년 1월 19일
If the array contains 4 elements and you want to add 360 elements in between, the final size should be 1080 instead of 1440. Taking into consideration that you want to respect the limits.
A= [1...(360 things)....2...(360 things)...3..(360 things)...4] .....360*3=1080.
Sancheet Hoque
Sancheet Hoque 2017년 1월 20일
oh yea thanks for the heads up!

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

채택된 답변

Stephen23
Stephen23 2017년 1월 20일
편집: Stephen23 2017년 1월 20일
@Sancheet Hoque: what you are trying to do is called interpolation. MATLAB has a nice collection of interpolation tools, and they are very easy to use. You only need to use the simplest one, interp1:
>> lat = [0;1;1;0];
>> lon = [2;3;4;3];
>> tarray = [0;10;20;30];
>> N = (numel(tarray)-1)*359 + numel(tarray);
>> t_new = linspace(tarray(1),tarray(end),N).';
>> lat_new = interp1(tarray,lat,t_new,'pchip');
>> lon_new = interp1(tarray,lon,t_new,'pchip');
>> plot(tarray,[lat,lon],'or',t_new,[lat_new,lon_new],'-b')
to create this:
  댓글 수: 2
Sancheet Hoque
Sancheet Hoque 2017년 1월 20일
thanks this was what I was looking for!
Abubakar Sani-Mohammed
Abubakar Sani-Mohammed 2019년 2월 19일
@ Stephen, that's a great effort, I believe i need similar script for my problem but still not able to figure out the right way. I have posted my Question on the forum and could be accessed through the link below;
I have attached my data as well, Any for of guidance would be appreciated. Thank you

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

추가 답변 (1개)

Jorge Mario Guerra González
Jorge Mario Guerra González 2017년 1월 19일
편집: Jorge Mario Guerra González 2017년 1월 19일
use the linspace function to add the values inbetween the array, also use an auxiliary variable. Maybe try this code.
tarray=[1000 2000 3000 4000];
spacing=360;
a=linspace(tarray(1),tarray(2),spacing);
new_tarray=linspace(tarray(1),tarray(2),spacing);
for i=2:length(tarray)-1
g=linspace(tarray(i),tarray(i+1),spacing+1);
new_tarray=[new_tarray g(2:end)];
end
tarray=new_tarray;
I know it's a bit rustic but does what I think you want. Also you can do the same fot the other arrays
  댓글 수: 1
Sancheet Hoque
Sancheet Hoque 2017년 1월 20일
I was thinking of doing something like this as well, until stephen showed me the
interp

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

카테고리

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