How do I assign values from 1D arrays to 4D arrays efficiently
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Chaitanya Sindagi
el 9 de Ag. de 2020
Comentada: J. Alex Lee
el 9 de Ag. de 2020
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
el 9 de Ag. de 2020
Editada: Chaitanya Sindagi
el 9 de Ag. de 2020
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.
Respuesta aceptada
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
Más respuestas (0)
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices 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!