How can I understand concatenation in multiple dimensions?

7 次查看(过去 30 天)
I have a 4-D cell array and I want to extend certain elements in the fourth dimension into the fifth dimension by concatenating an already-existing row from a two-dimensional cell array elsewhere in the workspace. I know I will probably need to use:
reshape(2-D,1,1,1,1,[]);
to redefine the array to point in the fifth dimension rather than the second, but I am having a lot of trouble understanding this logistically and would appreciate some help to determine which input arguments to use in the concatenation function. The elements in the 2-D row must be preserved in order.
  1 个评论
Stephen23
Stephen23 2018-8-14
编辑:Stephen23 2018-8-14
What sizes do the 4D and 2D arrays have?
What do you actually mean by "concatenation": is your goal to do something with the contents of each cell of the 4D array and the contents of the 2D array? Or are you actually wanting to concatenate the arrays themselves, like this:
A = [1,2,3];
B = [4,5,6];
C = cat(1,A,B)
C = 1 2 3
4 5 6

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2018-8-14
Ignoring some special cases involving the 0 x 0 empty array:
When you concatenate two arrays along the N'th dimension, all of the other dimensions they are defined on must be exactly the same.
For example, if you had a 3 x 2 and a 2 x 2, then the only valid way of concatenating them would be along the first dimension, creating a 5 x 2 result. You would not, for example, be able to concatenate them long the third dimension: that would be like stacking a 2 x 2 block on top of a 3 x 2 block, which MATLAB does not permit as it requires that all arrays be rectangular.
With your row of values being 1 x something, you could reshape the row to move the "something" to any dimension such as 1 x 1 x 1 x 1 x something, but you would only be able to concatenate that with something else on the 5th dimension of the other thing were 1 x 1 x 1 x 1 along the first four dimensions (and had no 6th or higher dimension).
You might need to repmat() the value as appropriate, such as
repmat( reshape(Row_Vector, 1, 1, 1, 1, []), size(Other_matrix,1), size(Other_Matrix,2), size(Other_matrix,3), size(Other_matrix,4), 1 )
and then since that would be the same dimensions for dimensions 1 through 4, you would be able to concatenate that along dimension 5.
  3 个评论
Stephen23
Stephen23 2018-8-14
编辑:Stephen23 2018-8-14
"How would I go about emptying all of the new elements, leaving only the one original row remaining, 'surrounded' by empty elements only there to allow concatenation to occur?"
The contents of the cells is irrelevant for concatenation of cell arrays. The cells can contain anything, and they certainly do not have to be empty to be concatenated:
>> C = {1,2,rand(99,100)};
>> D = {[],'hello world',NaN};
>> E = cat(1,C,D); % each cell different size and class, no problems!
Perhaps you mean something else by "concatenation": please explain in more detail what you are trying to achieve.
Nicole
Nicole 2018-8-14
编辑:Nicole 2018-8-14
  • I have a two-dimensional cell array, called 'engines', containing the names (first column) and performance characteristics (all other columns, numeric) of various engines.
  • I have a four-dimensional cell array, called 'ships', containing the names of various ships in the first column.
  • Each ship may have multiple stages (the first of several details on each ship, these details are stored along the second dimension). Each stage is assigned a number, and these numbers are indexed along the third dimension, with the first being in the second column of the row belonging to that ship.
  • Each stage of each ship may have multiple engines. In a FOR loop, the user inputs the number of engines on each stage. In another FOR loop within the first, the user inputs the names of each engine on each stage.
  • The names of each engine on each stage are stored, as strings, along the fourth dimension, with the first engine name of each stage replacing that stage's number along the third dimension (I have no need to store the actual number of stages and engines).
  • All of the above is working. Now I want the script to find, for each engine on each stage on each ship, the name of the engine in the first column of the 'engines' array, and store, along the fifth dimension, the performance characteristics of that engine under the name of the engine in question.
  • In an analogy using only three dimensions, this is essentially having rows for each stage, columns for each engine, and a third-dimensional extension from each element in those two dimensions containing the engine performance characteristics. The characteristics are extracted as row vectors from the 'engines' cell array and reshaped to 'point' in the third dimension (fifth in the actual scenario), then 'added' (concatenated?) onto the existing 'ships' array's second-dimension (fourth in the actual scenario) element.
Apologies if I am bad at explaining this concept. Once the 'ships' cell array is filled, other scripts called by the user should be able to extract the characteristics of any particular engine on any particular stage on any particular ship, allowing calculations like delta-v, burn time, TWR, etc. to be performed without the user having to input the ship's details each time.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by