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
Rafael Hernandez-Walls 2020년 7월 6일
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
Asrorkhuja Ortikov 2020년 7월 6일
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