Info
此问题已关闭。 请重新打开它进行编辑或回答。
Redistribution of elements of various arrays into a single multidimensional one
1 次查看(过去 30 天)
显示 更早的评论
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.
3 个评论
Steven Lord
2020-2-19
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.
回答(1 个)
Matt J
2020-2-19
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 个评论
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!