Azzera filtri
Azzera filtri

Creating an array of strings for mapping indexes to values

14 visualizzazioni (ultimi 30 giorni)
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 Commento
Stephen23
Stephen23 il 4 Mag 2019
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}

Accedi per commentare.

Risposta accettata

Jan
Jan il 29 Apr 2019
Modificato: Jan il 29 Apr 2019
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 Commento
altaf ahmed
altaf ahmed il 29 Apr 2019
Modificato: altaf ahmed il 4 Mag 2019
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!!!!

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by