Adding a singleton dimension to a 2D vector
52 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have a 22872x1 vector that needs to be a three dimensional matrix. Is there any way to add an additional singleton dimension to my vector in order to do such, i.e. go from 22872x1 to 22872x1x1? I need this vector to be three dimensional in order to run its values through a function, as the function requires a 3 dimensional matrix input.
0 comentarios
Respuestas (4)
Michael Rose
el 27 de Mayo de 2021
Editada: Michael Rose
el 27 de Mayo de 2021
Getting an Nx1x1 doesn't seem to work but getting a 1x1xN matrix works:
original_vector = ones(22872,1);
threeD_matrix(1,1,:) = a;
The variable, threeD_matrix, will now be size 1x1x22872. If you can deal with the vector being the last dimension, you can add as many singlton dimensions before it as you want in this fashion.
0 comentarios
James Tursa
el 19 de Jul. de 2018
Editada: James Tursa
el 19 de Jul. de 2018
There is no way to force a variable to physically store trailing singleton dimensions past the 2nd dimension. Operations that naturally result in such trailing singleton dimensions will automatically truncate the dimensions to 2D. Properly written MATLAB functions that need a value for a 3rd dimension that is singleton should account for that.
0 comentarios
Stephen Fox
el 6 de Jul. de 2022
Editada: Stephen Fox
el 6 de Jul. de 2022
%{
I encountered a similar problem in trying to use VideoWriter and associated functions. The writeVideo function requires a four dimensional array as input with the third dimension representing the number of "colors." However, gray scale image data is typicaly m x n x k where m and n are spatial dimensions, and k is a frame. In that case, the "missing" third dimension should be 1, and the input array is m x n x 1 x k. I used:
%}
v=VideoWriter('fileName.avi','Motion JPEG AVI');
open(v)
m = ones(256,256,1000); %One thousand frames of 256 x 256 video.
m4d(1,:,:,:) = m; %This is now j x m x n x k where j is a singleton dimension.
m4d = permute(m4d,[2,3,1,4]); %This is now in the correct input array format, m x n x 1 x k.
writeVideo(v,m4d);
close(v);
%{
Note that you can use permute() to great effect in manipulating dimensions, including singleton dimensions. Thus, you can create leading or trailing singleton dimensions and then rearrange them at will.
%}
2 comentarios
DGM
el 6 de Jul. de 2022
permute() is very useful for stuff like this, but ...
"Thus, you can create leading or trailing singleton dimensions and then rearrange them at will."
A = rand(1,1,1,10);
ndims(A)
B = permute(A,[1 4 2 3]);
ndims(B) % extraneous trailing singleton dimensions are discarded
As James Tursa's answer mentions.
Stephen Fox
el 6 de Jul. de 2022
Good point. "leading or trailing...rearrange at will" was too strong.
John Johnson
el 18 de En. de 2024
Editada: John Johnson
el 18 de En. de 2024
If you think you want a trailing singleton dimension you may be able to take advantage of the following fact:
If aMat is n dimensional then aMat(ind_1,ind_2,...ind_n,1) is the same as aMat(ind_1,ind_2,...,ind_n).
That is to say, indexing by too many dimensions is fine so long as the index has the value of 1.
1 comentario
DGM
el 19 de En. de 2024
Editada: DGM
el 19 de En. de 2024
That works when people write things like
% all the indexing is based on the actual array size
for r = 1:size(A,1)
for c = 1:size(A,2)
for p = 1:size(A,3)
B(r,c,p) = somefunction(A(r,c,p));
end
end
end
... but I don't think we're talking about the addressing process, but rather, artificially creating a nonstandard array representation to satisfy some existing function that demands that its inputs have a specific number of dimensions. We're working on the assumption that there is no reason to expect that 2D data wouldn't work with the operations done by said function.
I think the main problems are either presuming some number of pages (e.g. presuming that all images have 3 channels without actually checking), or doing things like this
% for some reason the author only cared about 3D inputs
% so you are simply not allowed to use it for anything else
if ndims(A)<3
error('no i won''t let you use this perfectly good array')
end
% even though the actual operations are not limited to 3D
% either in concept or in practice
B = 1-A;
Something similar to this last case is probably the likely problem. You can find plenty of code examples on the FEX that are crippled by similar artificial restrictions or hard-coded array sizes.
I'm with James on the matter. I can't think of a need to provide an extraneous trailing singleton dimension except to satisfy the demands of bad code. If there's no legitimate reason why the function shouldn't support 2D inputs, then its behavior is a bug. The solution is to fix the bug, not to attempt a fool's errand. Perhaps there are workarounds, but without knowing what operations are being performed, it's hard to say what would work.
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!