Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6389977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:28:14+00:00 2026-05-25T03:28:14+00:00

Possible Duplicate: MATLAB: How to vector-multiply two arrays of matrices? Is there a way

  • 0

Possible Duplicate:
MATLAB: How to vector-multiply two arrays of matrices?

Is there a way to contract higher-dimensional tensors in Matlab?

For example, suppose I have two 3-dimensional arrays, with these sizes:

size(A) == [M,N,P]
size(B) == [N,Q,P]

I want to contract A and B on the second and first indices, respectively. In other words, I want to consider A to be an array of matrices of size [M,N] and B to be equal length array of [N,Q] matrices; I want to multiply these arrays element-by-element (matrix-by-matrix) to get something of size [M,Q,P].

I can do this via a for-loop:

assert(size(A,2) == size(B,1));
assert(size(A,3) == size(B,3));

M = size(A,1);
P = size(A,3);
Q = size(B,2);

C = zeros(M, Q, P);
for ii = 1:size(A,3)
    C(:,:,ii) = A(:,:,ii) * B(:,:,ii);
end

Is there a way to do this that avoids the for-loop? (And perhaps works with arrays of an arbitrary number of dimensions?)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T03:28:14+00:00Added an answer on May 25, 2026 at 3:28 am

    Here is a solution (similar to what was done here) that computes the result in a single matrix-multiplication operation, although it involves heavy manipulation of the matrices to put them into desired shape. I then compare it to the simple for-loop computation (which I admit is a lot more readable)

    %# 3D matrices
    A = rand(4,2,3);
    B = rand(2,5,3);
    [m n p] = size(A);
    [n q p] = size(B);
    
    %# single matrix-multiplication operation (computes more products than needed)
    AA = reshape(permute(A,[2 1 3]), [n m*p])';      %'# cat(1,A(:,:,1),...,A(:,:,p))
    BB = reshape(B, [n q*p]);                         %# cat(2,B(:,:,1),...,B(:,:,p))
    CC = AA * BB;
    [mp qp] = size(CC);
    
    %# only keep "blocks" on the diagonal
    yy = repmat(1:qp, [m 1]);
    xx = bsxfun(@plus, repmat(1:m,[1 q])', 0:m:mp-1); %'
    idx = sub2ind(size(CC), xx(:), yy(:));
    CC = reshape(CC(idx), [m q p]);
    
    %# compare against FOR-LOOP solution
    C = zeros(m,q,p);
    for i=1:p
        C(:,:,i) = A(:,:,i) * B(:,:,i);
    end
    isequal(C,CC)
    

    Note that the above is performing more multiplications than needed, but sometimes “Anyone who adds, detracts (from execution time)”. Sadly this is not the case, as the FOR-loop is much faster here 🙂

    My point was to show that vectorization is not easy, and that loop-based solutions are not always bad…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
Possible Duplicate: How do I do multiple assignment in MATLAB? So let's say I
Possible Duplicate: How do you concatenate the rows of a matrix into a vector
Possible Duplicate: Growable data structure in MATLAB So in my current MATLAB script, I
Possible Duplicate: How do I iterate through each element in an n-dimensional matrix in
Possible Duplicate: How do I do multiple assignment in MATLAB? When dealing with cell
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: What Ruby IDE do you prefer? I've generally been doing stuff on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.