Adding new values in between existing values in an array
38 次查看(过去 30 天)
显示 更早的评论
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
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.
采纳的回答
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 个评论
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
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!