Borrar filtros
Borrar filtros

How to get rid of the for loop ?

1 visualización (últimos 30 días)
Robert Thiel
Robert Thiel el 9 de Sept. de 2015
Comentada: Robert Thiel el 10 de Sept. de 2015
the task
handles.plotdata.IR1 = zeros(1, 2001);
handles.plotdata.IR2 = zeros(1, 2001);
handles.plotdata.ax = zeros(1, 2001);
handles.plotdata.ay = zeros(1, 2001);
handles.plotdata.az = zeros(1, 2001);
my simplification so far
Kanal_name = {'IR1' 'IR2' 'ax' 'ay' 'az'};
for n = 1:length(Kanal_name)
handles.plotdata.(Kanal_name{n}) = zeros(1, 2001);
end
now i want to get rid of the for loop, any suggestions ?
  1 comentario
James Tursa
James Tursa el 9 de Sept. de 2015
Why do you want to get rid of the loop?

Iniciar sesión para comentar.

Respuesta aceptada

Kelly Kearney
Kelly Kearney el 9 de Sept. de 2015
Maybe this?
tmp = cell(2,length(Kanal_name));
tmp(1,:) = Kanal_name;
[tmp{2,:}] = deal(zeros(1,2001));
handles.plotdata = struct(tmp{:});
But this makes the code more difficult to read without adding any real benefit that I can see (the time difference is pretty negligible). Unless you have a definitive need to eliminate loops, I'd stick with your version.
  2 comentarios
per isakson
per isakson el 9 de Sept. de 2015
... or this
>> clear all
>> handles.plotdata = cell2struct( repmat({zeros(1, 2001)},1,5) ...
, {'IR1' 'IR2' 'ax' 'ay' 'az'}, 2 );
>> handles.plotdata
ans =
IR1: [1x2001 double]
IR2: [1x2001 double]
ax: [1x2001 double]
ay: [1x2001 double]
az: [1x2001 double]
Robert Thiel
Robert Thiel el 10 de Sept. de 2015
Thank you very much, both of you. Helps a lot!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by