Borrar filtros
Borrar filtros

Optimizing for loops by searching some data in cell arrays

4 visualizaciones (últimos 30 días)
Nik Rocky
Nik Rocky el 1 de Dic. de 2020
Comentada: Shubham Gupta el 7 de Dic. de 2020
I'm searching in multiple cell-array for some data:
for traj_cnt = 1:size(Traj_interpl,2) %for every Traj-Cell
for t_row_cnt = 1:size(Traj_interpl{traj_cnt},1) %for any row of Traj
if t_interpl(t_ref_cnt) == Traj_interpl{traj_cnt}(t_row_cnt,1) %if right row of Traj is choosen
motorspeed_detected(traj_cnt) = Traj_interpl{traj_cnt}(t_row_cnt,2); %write detected value to motorspeed_detected array
break; %just one success possible
motorspeed_detected(traj_cnt) = 0; % write 0 to motorspeed_detected array
Sometimes there are too much cells and rows, so is takes long long time and matlab in else state. What can I do to optimize a code?
traj_cnt is a variable of number of cells
Traj_interpl is a big cell array
t_row_cnt is a variable rows in one cell
t_interpl(t_ref_cnt) is a counting time variable (+10 ms) (initialisation outside)
Traj_interpl{traj_cnt}(t_row_cnt,1) is a saved time in cell arrays that I'm looking vor
Traj_interpl{traj_cnt}(t_row_cnt,2) is a saved motor value in cell array tham I try to save
motorspeed_detected array with detected values
So, I'm looking for special time in first column of cell arrays and if I find this, I take a value from second coloumn and save it to motorspeed_detected array.
How can I save resources? Thank you!
  2 comentarios
Mathieu NOE
Mathieu NOE el 2 de Dic. de 2020
so how did you generate that array ?
there is probably a way to do a validity test without for loops
Nik Rocky
Nik Rocky el 2 de Dic. de 2020
Hello, Array will be created by another "black box" script. I attach one of these files:

Iniciar sesión para comentar.

Respuestas (1)

Shubham Gupta
Shubham Gupta el 3 de Dic. de 2020
I tried cellfun() to get the desired output without using for loops:
x = cellfun(@(c)c(find(c==t_interpl(t_ref_cnt))+size(c,1)),Traj_interpl,'UniformOutput',false);
y = cellfun(@(c)~isempty(c),x);
motorspeed_detected = zeros(size(Traj));
It might not be the most efficient way to do this but it definitely improves efficiency of the code by removing for loops.
  2 comentarios
Nik Rocky
Nik Rocky el 3 de Dic. de 2020
Editada: Nik Rocky el 3 de Dic. de 2020
Hello Shubham! Thank you very much for your work! I try to implement your code but I get error:
z = cellfun(@(c)c(find(c==t_interpl(t_ref_cnt))+size(c,1)),Traj_interpl,'UniformOutput',false);
y = cellfun(@(c)~isempty(c),z);
motorspeed_detected = zeros(size(Traj));
Unable to perform assignment with 0 elements on the right-hand side.
Error in Analysis_algorithm_11_11_20_RealHW (line 207)
(I rename a x to z, because it will be allready used)
Do you see a possible error? See attatchement with Traj_interpl and t_interpl
Shubham Gupta
Shubham Gupta el 7 de Dic. de 2020
Sorry for being late to comment, I believe that sometimes "y" is a vector with logical zeros only. That means, you can't always find "t_interpl" inside "Traj_interpl", so you such have to detect such cases and using if....else condition execute last line only when there is at least one non-zero element. I hope it helps!

Iniciar sesión para comentar.


Más información sobre Multidimensional Arrays 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!

Translated by