How do I store function values in a vector while using a nested for loop?
Show older comments
I have the following code written. I need to find the values across a looped index for a specific equation. I need to, however, extrapolate data from a structure and then modify those individual values into a vector so that I can run the equation for all of the modified values. When I run this script, dz, dy, and dz do not come up as vectors, but as individual values. I've tried everything from changing the range of i to renaming all the variables and nothing as worked.
k=23;
v=NaN(length(drifter.mtime{1,k}));
x=drifter.long{1,k};
y=drifter.lat{1,k};
z=drifter.mtime{1,k};
for i=2:length(drifter.long{1,k})-1;
dx=x(i)-x(1);
dy=y(i)-y(1);
dz=z(i)-z(1);
for 1=2:length(drifter.long{1,k})-1;
v(i)=(sqrt(((dx(i)).^2)+((dy(i)).^2)))./(dz(i));
end
end
Answers (2)
KSSV
on 12 Oct 2017
k=23;
v=NaN(length(drifter.mtime{1,k}));
x=drifter.long{1,k};
y=drifter.lat{1,k};
z=drifter.mtime{1,k};
dx = zeros([],1) ;
dy = zeros([],1) ;
dz = zeros([],1) ;
for i=2:length(drifter.long{1,k})-1
dx(i)=x(i)-x(1);
dy(i)=y(i)-y(1);
dz(i)=z(i)-z(1);
for 1=2:length(drifter.long{1,k})-1;
v(i)=(sqrt(((dx(i)).^2)+((dy(i)).^2)))./(dz(i));
end
end
This is the way to save, there are some other mistake sin the code..please check them.
Andrei Bobrov
on 12 Oct 2017
Edited: Andrei Bobrov
on 12 Oct 2017
Maybe so?
k=23;
x=drifter.long{1,k};
y=drifter.lat{1,k};
z=drifter.mtime{1,k};
n = length(z);
A = [x(:),y(:),z(:)];
b = bsxfun(@minus,A(2:end-1,:),A(1,:));
v = hypot(b(:,1),b(:,2))./b(:,3);
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!