Store some data from one variable to another variable according to a condition
Informazioni
Questa domanda è chiusa. Riaprila per modificarla o per rispondere.
Mostra commenti meno recenti
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 Commenti
dpb
il 1 Lug 2020
Did you get the expected answer for a test case? Of course, writing it that way would have to loop over the vel() array.
Q? Is j anything other than 1 in vel(i,j) and/or dist(i,j)? How to write the code is dependent upon whether they are actual 2D arrays or 1D vectors. Your example appears to make look as though are just vectors, in which case, writing the second index is wasted effort.
There's an easier way but will wait to hear the answer to the Q? first... :)
Angela Marino
il 1 Lug 2020
Modificato: dpb
il 1 Lug 2020
MaryD
il 1 Lug 2020
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
il 1 Lug 2020
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...
Risposte (0)
Questa domanda è chiusa.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!