How do I store function values in a vector while using a nested for loop?

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

回答(2 个)

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.
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);

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by