need help to reduce time for each loop

i have 2 tables taxidata.pickup and TaxiRegionsandZones.pickup, i want to compare those two col and if they match it should creat a new col in taxidata.pickupzones (as TaxiRegionsandZones is categorical ), the new taxidata.pickupzones should have the name to that category.
for k = 1 : length(taxidata.PickupZone)
% for Lower Manhattan
for i = 1: length(TaxiRegionsandZones.LowerManhattan)
if taxidata.PickupZone(k) == TaxiRegionsandZones.LowerManhattan(i)
taxidata.PickupRegion(k) = "LowerManhattan";
end
end
i have tried this but it is taking a vary long time, is there any method to reduce the time

回答 (1 件)

KSSV
KSSV 2021 年 3 月 26 日

0 投票

You need not to run loop for this. Read about function ismemeber, ismembertol, knnsearch.
[lia,lob] = ismember(taxidata.PickupZone,TaxiRegionsandZones.LowerManhattan) ;

4 件のコメント

the king
the king 2021 年 3 月 26 日
so how should i add that to my table
Rik
Rik 2021 年 3 月 26 日
Did you experiment with what ismember does? Did you read the documentation?
the king
the king 2021 年 3 月 26 日
yea, i am getting lob as index but just not getting how to add that to table.
i am new to matlab.
Rik
Rik 2021 年 3 月 26 日
Can you see how lia and lob are related to the values of k and i for which you did the assignment?
If you have trouble with Matlab basics you may consider doing the Onramp tutorial (which is provided for free by Mathworks).

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2021 年 3 月 26 日

コメント済み:

Rik
2021 年 3 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by