Info

此问题已关闭。 请重新打开它进行编辑或回答。

Store some data from one variable to another variable according to a condition

1 次查看(过去 30 天)
Hi everyone, I have a little problem.
I have defined these two variables
vel(i,j) (exemple vel=[1 2 3 0 0 0 0 2 1 3 etc])
dist(i,j)
%i,j>>27
pos=ones(27,1)
I would like to store the given "dist" (i,1) corresponding "vel" == 0 only for the first value ==0
if vel(i,1)==0 && vel(i-1,1)~=0
pos(:,1)=dist(i,1);
end
Is the code correct to have what I want?
  4 个评论
MaryD
MaryD 2020-7-1
This not looks like it's going to work as you want only first value.
[index]=find(vel(:,1)==0,'first');
pos(:,1)=dist(index,1);
Try something like this instead
dpb
dpb 2020-7-1
I fixed indenting to be able to at least see where the loops start/end but no real idea what is trying to be done...
...
%I identify the position of stops R2 and update the array
if veloc(i,1)==0 & veloc(i-1,1)~=0
pos_fermateR2(:,1)==distanza(i,1);
end
end % loop j
is inside the inner loop on both i and j so there's the question of what you're looking for and what subscripts mean...guessing, it looks like maybe i is over some time step and j some data set...but that's purely guessing.
veloc(i,1) and veloc(i-1,1) are each only addressing a single element in an array; and it looks like the (i,j) position is being set inside the loop.
I'm guessing you really want to move this to be after the j loop completes and then look over the i vector of veloc(i,:)
If that were correct, use something like
ixstop=find(veloc(i,:)==0,1);
pos_fermateR2(i)==distanza(i,ixstop);
But, this is really guessing about what the code really does...

回答(0 个)

此问题已关闭。

标签

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by