Adding new values in between existing values in an array

38 Ansichten (letzte 30 Tage)
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 Kommentare
Jorge Mario Guerra González
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 am 20 Jan. 2017
oh yea thanks for the heads up!

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 20 Jan. 2017
Bearbeitet: Stephen23 am 20 Jan. 2017
@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 Kommentare
Sancheet Hoque
Sancheet Hoque am 20 Jan. 2017
thanks this was what I was looking for!
Abubakar Sani-Mohammed
Abubakar Sani-Mohammed am 19 Feb. 2019
@ 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Jorge Mario Guerra González
Bearbeitet: Jorge Mario Guerra González am 19 Jan. 2017
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 Kommentar
Sancheet Hoque
Sancheet Hoque am 20 Jan. 2017
I was thinking of doing something like this as well, until stephen showed me the
interp

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by