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
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
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
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!