フィルターのクリア

Creating an array of strings for mapping indexes to values

14 ビュー (過去 30 日間)
altaf ahmed
altaf ahmed 2019 年 4 月 29 日
コメント済み: Stephen23 2019 年 5 月 4 日
In Python it is easy to implement but wondering how can we do it in MATLAB. Can someone trnslate this toi MATLAB code.
stationmap = [S0,S1,S2,S3,S4,S5,S6,S7,S8,S9]
for i in range(10000):
Station_ID = stationmap[i % 10]
print("For Time {0}, we have station {1}".format(i, Station_ID))
  1 件のコメント
Stephen23
Stephen23 2019 年 5 月 4 日
This is MATLAB, so you can easily get rid of the loop:
>> V = 1:10000;
>> C = {'S0', 'S1', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8', 'S9'};
>> T = [num2cell(V);C(1+mod(V-1,numel(C)))];
>> fprintf('For Time {%d}, we have station {%s}\n',T{:})
For Time {1}, we have station {S0}
For Time {2}, we have station {S1}
For Time {3}, we have station {S2}
For Time {4}, we have station {S3}
For Time {5}, we have station {S4}
For Time {6}, we have station {S5}
For Time {7}, we have station {S6}
For Time {8}, we have station {S7}
For Time {9}, we have station {S8}
For Time {10}, we have station {S9}
For Time {11}, we have station {S0}
... lots more lines here
For Time {9993}, we have station {S2}
For Time {9994}, we have station {S3}
For Time {9995}, we have station {S4}
For Time {9996}, we have station {S5}
For Time {9997}, we have station {S6}
For Time {9998}, we have station {S7}
For Time {9999}, we have station {S8}
For Time {10000}, we have station {S9}

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

採用された回答

Jan
Jan 2019 年 4 月 29 日
編集済み: Jan 2019 年 4 月 29 日
What about:
stationmap = {'S0', 'S1', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8', 'S9'}
% Alternatively:
% stationmap = sprintfc('S%d', 0:9);
for k = 1:10000
fprint('For Time {%d}, we have station {%s}\n', ...
k, stationmap{mod(k, 10} + 1));
end
  1 件のコメント
altaf ahmed
altaf ahmed 2019 年 4 月 29 日
編集済み: altaf ahmed 2019 年 5 月 4 日
stationmap = {'S0', 'S1', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8', 'S9'}
% Alternatively:
% stationmap = sprintfc('S%d', 0:9);
for k = 1:10000
fprintf('For Time {%d}, we have station {%s}\n', ...
k, stationmap{mod(k, 10) + 1});
end
Thanks a lot!!!!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCall Python from MATLAB についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by