Borrar filtros
Borrar filtros

Creating an array of strings for mapping indexes to values

14 visualizaciones (últimos 30 días)
altaf ahmed
altaf ahmed el 29 de Abr. de 2019
Comentada: Stephen23 el 4 de Mayo de 2019
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 comentario
Stephen23
Stephen23 el 4 de Mayo de 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}

Iniciar sesión para comentar.

Respuesta aceptada

Jan
Jan el 29 de Abr. de 2019
Editada: Jan el 29 de Abr. de 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 comentario
altaf ahmed
altaf ahmed el 29 de Abr. de 2019
Editada: altaf ahmed el 4 de Mayo de 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!!!!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Call Python from MATLAB en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by