Making a list from two arrays
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Julien
el 12 de Ag. de 2014
Comentada: Julien
el 12 de Ag. de 2014
I'm relatively new to coding, and have no clue how I would pull the following off.
Say I have two vectors: A = [a b c d] and B = [1 2 3 4].
I would like to make a function that takes these two as inputs and outputs exactly the following into a .txt file, curly brackets included:
{a->1,b->2,c->3,d->4}
I was thinking some kind of for loop, but I have no experience using them and no idea how I would set this up.
Thanks
0 comentarios
Respuesta aceptada
Azzi Abdelmalek
el 12 de Ag. de 2014
Editada: Azzi Abdelmalek
el 12 de Ag. de 2014
A = {'a' 'b' 'c' 'd'}
B = [1 2 3 4]
out='{'
for k=1:numel(B)
out=[out sprintf('%s->%d,',A{k},B(k))]
end
out(end)='}'
%or
A = {'a' 'b' 'c' 'd'}
B = [1 2 3 4]
out=['{' strjoin(cellfun(@(x,y) sprintf('%s->%d',x,y),A,num2cell(B),'un',0),',') '}']
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!