How do I assign values from 1D arrays to 4D arrays efficiently

2 visualizaciones (últimos 30 días)
I have multiple 4D arrays where each dimension is the slip ratio of one wheel of a car. Each array stores a parameter's value at every combination of slip ratios from all 4 wheels. I do this so I can use vectorization to greatly speed up the performance of my calculations by using element-wise operations. However, this requires me to have some basic pre-filled arrays to perform my computations on. These include arrays that simply contain slip ratios and forces which only vary in one dimension for each tyre. Currently I'm filling these in like so-
sr_arr_size=8
Slip_ratio_FL = zeros(sr_arr_size,sr_arr_size,sr_arr_size,sr_arr_size); %Define like this for all 4D arrays used below
for i = 1:sr_arr_size
slip_ratio = slip_ratio_set(i);
Slip_ratio_FL(i,:,:,:) = slip_ratio;
Slip_ratio_FR(:,i,:,:) = slip_ratio;
Slip_ratio_RL(:,:,i,:) = slip_ratio;
Slip_ratio_RR(:,:,:,i) = slip_ratio;
Fx_FL(i,:,:,:) = Fx_FL_sr(i);
Fx_FR(:,i,:,:) = Fx_FR_sr(i);
Fx_RL(:,:,i,:) = Fx_RL_sr(i);
Fx_RR(:,:,:,i) = Fx_RR_sr(i);
Fy_FL(i,:,:,:) = Fy_FL_sr(i);
Fy_FR(:,i,:,:) = Fy_FR_sr(i);
Fy_RL(:,:,i,:) = Fy_RL_sr(i);
Fy_RR(:,:,:,i) = Fy_RR_sr(i);
end
Here, dimension-1 is FL(frontLeft tyre), dimension-2 is FR(frontRight tyre) etc etc...
slip_ratio_set, Fx_FL_sr, ...., Fy_RR_sr are all 1D arrays indexed by i, which I'm trying to fill into the 4D equivalents of themselves
Is there a more efficient and cleaner way of filling these? Currently this takes about 10-15% of my total runtime
  12 comentarios
Chaitanya Sindagi
Chaitanya Sindagi el 9 de Ag. de 2020
Editada: Chaitanya Sindagi el 9 de Ag. de 2020
I tried doing this
a = 15;
Fx_FL_sr = rand(a,1);
Fx_FR_sr = rand(a,1);
Fx_RL_sr = rand(a,1);
Fx_RR_sr = rand(a,1);
for i = 1:a
Fx_FL(i,:,:,:) = Fx_FL_sr(i);
Fx_FR(:,i,:,:) = Fx_FR_sr(i);
Fx_RL(:,:,i,:) = Fx_RL_sr(i);
Fx_RR(:,:,:,i) = Fx_RR_sr(i);
end % Takes 0.25ms
[Fx_FL_nd, Fx_FR_nd, Fx_RL_nd, Fx_RR_nd] = ndgrid(Fx_FL_sr,Fx_FR_sr,Fx_RL_sr,Fx_RR_sr); % Takes 0.165ms
for i = 1:length(Fx_FL_1d)
[fl,fr,rl,rr] = ind2sub([a,a,a,a], i); % 287ms
Fx_FL_1d(i) = Fx_FL_sr(fl); % ~2ms
Fx_FR_1d(i) = Fx_FR_sr(fr); % ~2ms
Fx_RL_1d(i) = Fx_RL_sr(rl); % ~2ms
Fx_RR_1d(i) = Fx_RR_sr(rr); % ~2ms
end
ind2sub is so slow I'm wondering what I'm getting wrong here
I'm not quite sure what the benefit of putting the 4 wheels in a cell array would be?
EDIT:
I replaced ind2sub with a manual approach and it takes a lot less time, but still an order of magnitude or slower than the other methods
for i = 0:(length(Fx_FL_1d)-1)
Fx_FL_1d(i+1) = Fx_FL_sr(mod(floor(i/a^3), a) +1); % 2ms
Fx_FR_1d(i+1) = Fx_FR_sr(mod(floor(i/a^2), a) +1); % 2.6ms
Fx_RL_1d(i+1) = Fx_RL_sr(mod(floor(i/a), a) +1); % 3ms
Fx_RR_1d(i+1) = Fx_RR_sr(mod(i,a)+1); % 3.2ms
end % 1.6ms
J. Alex Lee
J. Alex Lee el 9 de Ag. de 2020
Well, looks like Matt J's answer is the one you need, since it looks like it turns out you actually did want to do some math in matrix-like sense on the data itself. In this case, I agree cells would not have helped.

Iniciar sesión para comentar.

Respuesta aceptada

Matt J
Matt J el 9 de Ag. de 2020
Editada: Matt J el 9 de Ag. de 2020
I do this so I can use vectorization to greatly speed up the performance of my calculations by using element-wise operations. However, this requires me to have some basic pre-filled arrays to perform my computations on.
No, not if you have Matlab R2016b or higher. If so, you can just organize your arrays as vectors along each of the relevant dimensions, e.g. Nx1x1x1 and 1xNx1x1 and 1x1xNx1 and 1xNx1x1 and then you rely on implicit expansion when you do the array arithmetic. This will be much faster and consume much less memory . My ndgridVecs utility
may be useful for setting up the initial arrays. It works much the same as ndgrid,
[Slip_ratio_FL, Slip_ratio_FR, Slip_ratio_RL, Slip_ratio_RR] = ndgridVecs(slip_ratio_set);
As an example, this will compute all sums of quadruplets [a,b,c,d] of numbers between 1 and 3,
[A,B,C,D]=ndgridVecs(1:3);
S=A+B+C+D; %sums of all combinations
  1 comentario
Chaitanya Sindagi
Chaitanya Sindagi el 9 de Ag. de 2020
Wow this is amazing, I had no idea about implicit expansion. My code went from being O(n^4) to around O(n), not to mention the memory savings, I had nearly 30 4D arrays, now I have just a few that I cannot avoid.
Thank you so much!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by