필터 지우기
필터 지우기

How do you store data created in a loop in a vector

조회 수: 1 (최근 30일)
Colin Munch
Colin Munch 2015년 10월 9일
댓글: Colin Munch 2015년 10월 9일
The problem is calculating the speed of a car based off of the time it take for a sensor to read the front and back wheels, which in turn is based off of the distance between the 2. here's the code
%input number of cars driven over the cable
ncars=input('how many cars have driven over the sensor today, must be >2: ');
while ncars<=2 || round(ncars) ~= ncars
ncars=input('ERROR invalid number of cars, must be >2, try again : ');
end
%set up speed limit specification
speedlimit=input('\n what is the speed limit for the specified road, must be >10 : '); %mph
while speedlimit <= 10 || round(ncars) ~= ncars
speedlimit=input('\n ERROR invalid entry for speedlimit (must be >10), try again : ');
end
%generate random wheelbase between 104 and 106 inches
wheelbaseoriginal=rand(104*2);
%convert wheelbase to miles
wheelbase=(wheelbaseoriginal/12)/5280;
%initiate loop for number of cars
total=0;
carnumber=1
%initiate vector to be updated later
velocities=[ncars];
for ncars=1:1:ncars
%generate random time difference for front to back tires
tlow=wheelbase/speedlimit*1.2;
thigh=wheelbase/speedlimit*0.9;
wheeltime=tlow+thigh*rand;
%calculate the speed of each car
speed=wheeltime*3600;
velocities(carnumber)=speed;
total= speed+total;
carnumber=carnumber+1
end

채택된 답변

Robert Dylans
Robert Dylans 2015년 10월 9일
편집: Robert Dylans 2015년 10월 9일
To store data in the vector, use the variable that is being used for the loop as the vector index. data(n)=answer It's always a good idea to preallocate a vector of the expected vector size created in the loop, to run the code more efficiently.
total=0;
velocities=zeros(ncars,1);
for n=1:ncars
tlow=wheelbase/speedlimit*1.2;
thigh=wheelbase/speedlimit*0.9;
wheeltime=tlow+thigh*rand;
speed=wheeltime*3600;
velocities(n)=speed;
total=total+speed;
end
Some errors in the original code:
You were using "ncars" as the loop variable. This will overwrite the value you had the user input earlier.
For loops without an increment default to "1". ie. n=1:1:10 is the same as n=1:10. Not really an error, just an fyi.
The variable "carnumber" was redundant, the same as your loop variable (n).
Early in your code you have for the wheelbase rand(104). This will generate a 104x104 matrix of random variables. You want it to be 104+2*rand for random value between 104-106.
  댓글 수: 5
Robert Dylans
Robert Dylans 2015년 10월 9일
On the fprint line at the end of your loop, you're using these:
carnumber(n),wheelbase(n),speed(n)
But they're defined as:
speed=wheeltime*3600;
carnumber=carnumber+1;
wheelbase=(wheelbaseoriginal/12)/5280;
These three are defined as single value variables. Not vectors. So, in the second loop, it will try to access speed(2), aka the second value in the vector 'speed', which doesn't exist. To fix that, just remove the "(n)" from those 3.
Colin Munch
Colin Munch 2015년 10월 9일
rog. thank you sir. you have saved my ass 3 times in an hour. haha

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by