insert singelton dimension for broadcasting

Let A be of size (M x N x P) and B be of size (M x N x K). What's the most conveniant way to broadcast C = A.*B such that C is of size (M x N x P x K) ?
In python it's
C = A[:,:,np.newaxis,:]*B[:,:,:,np.newaxis]
I can get it working by using reshape so that A is of (M x N x 1 X P), but it's a bit annoying because

Risposte (3)

You could also create your own specialized function that does it -
[M,N,P,K]=deal(2,3,4,5);
A=rand(M,N,P);
B=rand(M,N,K);
C=tensortimes(A,B);
size(C)
ans = 1×4
2 3 4 5
function C=tensortimes(A,B)
[m,n,p]=size(A);
[mm,nn,k]=size(B);
assert(mm==m && nn==n, 'First 2 dimensions don''t match');
C=A.*reshape(B,m,n,1,k);
end
Catalytic
Catalytic il 23 Mar 2023
Modificato: Catalytic il 23 Mar 2023
Inserting dimensions seems like as much a pain as reshape, but if you must do it that way, here's an approach closer to the Python style -
[M,N,P,K]=deal(2,3,4,5);
A=rand(M,N,P);
B=rand(M,N,K);
C=A.*newdim(B,3); size(C)
ans = 1×4
2 3 4 5
function A=newdim(A,n)
%insert a singleton dimension at one or more locations in size(A),
%designated by vector n.
n0=ndims(A);
n=unique(n);
N=max(n0+numel(n),max(n));
dims=nan(1,N);
dims(n)=1;
nanlocs=find(isnan(dims));
dims(nanlocs(1:n0))=size(A);
dims(isnan(dims))=1;
A=reshape(A,dims);
end

4 Commenti

if you need to write a function like newdim to do so, then yeah.
In the python example, effectively np.newaxis does this, but temporarily and doesn't require holding any dimension sizes in the local scope.
It's temporary with the newdim function also. I don't think the Python style has fewer keystrokes either, although I guess that depends how you name the function...
the beauty is in python, you don't need to know the size of the dimension you are re-arranging, just the location or index. At minimum it saves 1 line of code, and reduces dummy temp variables.
If MATLAB by default had the newdim.m functionality you proposed, that'd be I think very intuitive, or if they implemented a A(:,:,newaxis) slice tool even better
If MATLAB by default had the newdim.m functionality you proposed, that'd be I think very intuitive
This seems to imply that the newdim function does what you want. If so, I wonder if you'd consider clicking Accept. From the standpoint of your convenience, it shouldn't matter whether the function is provided by MathWorks or by me.

Accedi per commentare.

Catalytic
Catalytic il 23 Mar 2023
Modificato: Catalytic il 23 Mar 2023
You didn't complete your description of why reshape() is "a bit annoying". If you're going to be doing the same operation repeatedly, this can cut down on the syntax somewhat (by reusing I) -
[M,N,P,K]=deal(2,3,4,5);
A=rand(M,N,P);
B=rand(M,N,K);
I=reshape(1:M*N*K, M,N,1,K);
C=A.*B(I);
size(C)
ans = 1×4
2 3 4 5

5 Commenti

i suppose need to access the dimensions as variables is inconvenient, as you see in the python example, this is avoided by using the colon object as in A[:,:,np.newaxis,:]
but so I suppose
[M,N,P] = size(A)
A.*reshape(B,[M,N,1,P])
it's a little cleaner i suppose
There's always permute().
[M,N,P,K]=deal(2,3,4,5);
A = rand(M,N,P);
B = rand(M,N,K);
C = A.*permute(B,[1 2 4 3]);
size(C)
ans = 1×4
2 3 4 5
yeah this is the one-liner solution I was looking for!
Thanks!
Yes, but for this you need to keep track of the total number of dimensions, whereas with newdim, you do not.
DGM
DGM il 29 Mar 2023
Modificato: DGM il 29 Mar 2023
You are correct. What's frustrating to automation is that despite MATLAB arrays implicitly having infinite trailing singleton dimensions, you can't borrow arbitrarily from them in a call to permute(). The permutation vector must contain no gaps or repetition, so you can effectively only borrow from the dims(X)+1 dimension.
Still, it's important to the novice to get familiar with both reshape() and permute(), especially if they're starting to learn how to use one without realizing the power of using both.

Accedi per commentare.

Richiesto:

il 23 Mar 2023

Modificato:

DGM
il 29 Mar 2023

Community Treasure Hunt

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

Start Hunting!

Translated by