3 views (last 30 days)

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?

Thank you for your attention.

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 think the loop you've shown would work just fine, but a quicker way would be

array2=repelem(reshape(array1,1,1,[]), 200,4);

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/506421-redistribution-of-elements-of-various-arrays-into-a-single-multidimensional-one#comment_799643

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/506421-redistribution-of-elements-of-various-arrays-into-a-single-multidimensional-one#comment_799643

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/506421-redistribution-of-elements-of-various-arrays-into-a-single-multidimensional-one#comment_799645

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/506421-redistribution-of-elements-of-various-arrays-into-a-single-multidimensional-one#comment_799645

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/506421-redistribution-of-elements-of-various-arrays-into-a-single-multidimensional-one#comment_799666

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/506421-redistribution-of-elements-of-various-arrays-into-a-single-multidimensional-one#comment_799666

Sign in to comment.