How to calculate distance till the next possible stop?

1 次查看(过去 30 天)
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

采纳的回答

Rafael Hernandez-Walls
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 个评论
Asrorkhuja Ortikov
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?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by