Creating an array of strings for mapping indexes to values

4 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by