How to calculate distance till the next possible stop?

1 view (last 30 days)
Is there any formula or ways to calculate distance between current parking place and next possible parking with given data of all durations during the day of a car? let's say on one column I have parking locations, and the other driving distances between stops, but parking is only possible at Home or Workplace, not shopping mall or restaurant.
parkinglocations = ["Home" "mobile" "ShoppingMall" "mobile" "Workplace" "mobile" "Home"]'; % when "mobile" car is driving
driving_distance = [0 35 0 15 0 40 0]';
with these data I want to generate range of a car till next stop where parking is possible, and result should be
next_range = [50 0 0 0 40 0]';
I have tried with cumsum and sum functions like below, but seems like not working. And I don't know how to use complex functions yet, can anybody help please?
next_range = zeros(size(parkinglocations,1),1);
parking_binary = zeros(size(parkinglocations,1),1);
for i=1:length(parkinglocations)
if parkinglocations(i) == "Home" || parkinglocations(i) == "Workplace"
parking_binary(i) = 1;
else
parking_binary(i) = 0;
end
next_range = cumsum(distance(parking_binary==0));
end

Accepted Answer

Rafael Hernandez-Walls
Rafael Hernandez-Walls on 6 Jul 2020
I don't know is correct,
parkinglocations = ["Home" "mobile" "ShoppingMall" "mobile" "Workplace" "mobile" "Home"]'; % when "mobile" car is driving
driving_distance = [0 35 0 15 0 40 0]';
next_range = zeros(size(parkinglocations));
parking_binary = zeros(size(parkinglocations));
for i=1:length(parkinglocations)
if parkinglocations(i) == "Home" || parkinglocations(i) == "Workplace"
parking_binary(i) = 1;
else
parking_binary(i) = 0;
end
end
% New section
n=find(parking_binary==1)';
n(end+1)=n(end);
s=[];r=1;
for k=1:length(n)-1
s=[s,sum(driving_distance(n(k):n(k+1)-1))];
end
sol=parking_binary';
sol(logical(parking_binary))=s;
sol=sol(1:end-1)
  1 Comment
Asrorkhuja Ortikov
Asrorkhuja Ortikov on 6 Jul 2020
it did work :) like a charm. But is there any staightforward formulas that for expamle, does calcuation of sum of values when parking_binary is zero and puts that sum into every single parking_binary = 1 positions before summation?

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!