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

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

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... :)
This is the original code
for c=1:num_simulazioni
%random value generation
simulazione=rand();
%condition for choosing the sumo file
if simulazione < 0.5
system(['sumo -c' 'C:\Users\angel\Desktop\prova_flussi\simulazione_57\r2_traci_57.sumocfg &']);
else
system(['sumo -c' 'C:\Users\angel\Desktop\prova_flussi\simulazione_79\r2_traci_79.sumocfg &']);
end
[traciVersion, sumoVersion]= traci.init(8873);
simulazione=round(simulazione);
%Variable definitions
veicoli_controllati={'R2.0'; 'R2.1';'R2.2';'R2.3'};
tempo_simulazione=7200;
%initialization of variables for machine learning
veloc_ML=zeros(27,length(veicoli_controllati));
pers_ML=zeros(27,length(veicoli_controllati));
check_point=zeros(28,length(veicoli_controllati));
%stopped positions R2 bus
pos_fermateR2=ones(28,1);
f=1;
%for loop for calculating values
for i = 1:tempo_simulazione
traci.simulationStep();
%I read vehicles on the net
VEHID=traci.vehicle.getIDList();
for j=1:length(veicoli_controllati)
%I read the information relating to the vehicle investigated
if ismember(veicoli_controllati{j},VEHID)==1
pos=traci.vehicle.getPosition(veicoli_controllati{j}); %position bus
Xpos(i,j)=pos(1);
Ypos(i,j)=pos(2);
num_persone(i,j)=traci.vehicle.getPersonNumber(veicoli_controllati{j}); %capacity bus
veloc(i,j)=traci.vehicle.getSpeed(veicoli_controllati{j}); %velocity bus
distanza(i,j)=traci.vehicle.getDistance(veicoli_controllati{j}); %curvilinear abscissa
%conversion x,y in lat,long
[lon(i,j), lat(i,j)]=traci.simulation.convertGeo(Xpos(i,j),Ypos(i,j));
else
Xpos(i,j)=NaN;
Ypos(i,j)=NaN;
num_persone(i,j)=NaN;
veloc(i,j)=NaN;
distanza(i,j)=NaN;
lat(i,j)=NaN;
lon(i,j)=NaN;
end
%%
%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
%internal if loop to understand which stops are between
if f==pos_fermateR2
check_point(f,j)=i;
if f>1
veloc_ML(m,j)=mean(veloc(check_point(f-1,j):check_point(f,j),j));
pers_ML(m,j)=mean(num_persone(check_point(f-1,j):check_point(f,j),j));
%m is the interval between two stops. Value n_stop-1
end
end
end % loop i
end
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
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.

Prodotti

Release

R2020a

Richiesto:

il 1 Lug 2020

Chiuso:

il 20 Ago 2021

Community Treasure Hunt

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

Start Hunting!

Translated by