# Redistribution of elements of various arrays into a single multidimensional one

3 views (last 30 days)
Richard Wood on 19 Feb 2020
Edited: Richard Wood on 19 Feb 2020
Hello everyone,
I am trying to figure it out how to associate each element of a one-dimensional array (let's call it array1), let's say, 4000x1, to one of the dimensions of a three-dimensional array (let's call it array2) of, let's say, 200x4x4000 elements, so that the last index of this array adquires the elements of the one-dimensional array. Without too many details, because this is nested in some for loops, what I am trying to do it is something like this:
for i=1:length(array1)
array2(:,:,i)=array1(i);
end
I think that this does not work properly for my purpose, right? If that it is the case, why and how could I try to mantain the same for approach achieving what I want?
Edited: my problem is more intricate, and I was mistaken about which is the critical point. Let's imagine that you have the three following arrays: physical_time_elastic (2431x1 elements), file_magnetization_components_elastic (72932431x3 elements) and spatial_grid (30001x1 elements). I want to create a three-dimensional array where everything is well distributed, because my data is not well distributed coming from simulations. I want to create an array data_set_elastic (data_set_elastic=zeros(length(spatial_grid),length(file_magnetization_components_elastic(1,:))+1,length(physical_time_elastic))). Here, I want to place the data of physical_time_elastic in the third dimension of data_set_elastic. In addition, on each two-dimensional plane for each element of physical_time_elastic I want to introduce in the first element of the second index of data_set_elastic the spatial_grid array. In the rest of elements of the second index of data_set_elastic I want to place elements as follows: for the first element of physical_time_elastic the first 30001 elements of each arrow of file_magnetization_components_elastic, being, the first of file_magnetization_components_elastic the second one in the second dimension of data_set elastic, the second of file_magnetization_components_elastic the third one in data_set_elastic and the same logic for the last element of it. In the second element of physical_time_elastic, I want to place, in the second to fourth element of the second index of data_set_elastic, the elements that goes from the 30002 element of file_magnetization_components_elastic element to 60002 element, and so on up to the end of this array. To do this, I have created the following script:
data_set_elastic=zeros(length(spatial_grid),length(file_magnetization_components_elastic(1,:))...
+1,length(physical_time_elastic));
for i=1:length(physical_time_elastic)
k=((i-1)*length(spatial_grid)+1):(i*length(spatial_grid));
for l=k(1):k(end)
for j=2:(length(file_magnetization_components_elastic(1,:))+1)
data_set_elastic(l-length(spatial_grid)*(i-1),j,i)=...
file_magnetization_components_elastic(l,j-1);
end
end
data_set_elastic(:,:,i)=physical_time_elastic(i);
for m=1:length(spatial_grid)
data_set_elastic(m,1,i)=spatial_grid(m);
end
end
Apparently, in this way, I do not obtain what I am looking for.
Any suggestion?
If someone knows how to do it with reshape, I will be happy to try to understand that approach to reduce computational time.

Matt J on 19 Feb 2020
I recommend giving an example with small array sizes.
Steven Lord on 19 Feb 2020
I second Matt J's recommendation. For example, what exactly would you expect to be the result if you did what you proposed with the following smaller arrays? If possible show us not only the final result but also describe in terms of the inputs exactly how you obtained that result.
physical_time_elastic = (1:24).';
file_magnetization_components_elastic = reshape(100 + (1:(72*3)), [72 3]);
spatial_grid = (200:300).';
physical_time_elastic is 24-by-1, file_magnetization_components_elastic is 72-by-3, and spatial_grid is 101-by-1. That should give you enough data to show the pattern without having to show thousands upon thousands of points.
Richard Wood on 19 Feb 2020
Thank you for your comments. I attach an example, as you have suggested. For example, after having my final array data_set_elastic, due to the way it has been written in my code, the element file_magnetization_components_elastic(300,2) should be the same as data_set_elastic(100,3,3), but that condition is not fulfilled. With that kind of checking I was trying to figure it out if I achieve what I wanted to write, and apparently that is not the case.
The criteria now it is that each 100 elements of file_magnetization_components_elastic, I have to move to the following third dimension element of data_set_elastic to have my 100x4 bidimensional array.
I just want to create an array that gives, for a space position and a instant of time, the three magnetization components, if that helps to understand the motivation. Simulations gives magnetization components in a bidimensional array, so the first 100 rows of the magnetization data corresponds to all the spatial position for t=t1, the first time step. I think that that clarifies the context. The three columns of the magnetization corresponds to the x-th, y-th and z-th components of that vector. I want also to put as the first column the spatial value for each time step, which is not present in the original magnetization file.

Matt J on 19 Feb 2020
I think the loop you've shown would work just fine, but a quicker way would be
array2=repelem(reshape(array1,1,1,[]), 200,4);

#### 1 Comment

Richard Wood on 19 Feb 2020
Yeah, thank you. You are right. I was wrong about which was the problem. See my question above, because I have edited it explaining the real problem, if you want.