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 件)

KSSV
KSSV 2017 年 10 月 12 日
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
Andrei Bobrov 2017 年 10 月 12 日
編集済み: Andrei Bobrov 2017 年 10 月 12 日
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 ExchangeLoops and Conditional Statements についてさらに検索

製品

質問済み:

2017 年 10 月 12 日

編集済み:

2017 年 10 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by