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

  • SEARCH
  • Home
  • 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 8303461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:32:57+00:00 2026-06-08T17:32:57+00:00

I hope my problem has a very simple solution. I just can’t find it:

  • 0

I hope my problem has a very simple solution. I just can’t find it:

Assume you have two vectors (one a column vector, one a row vector) A, B:

A = [1,2,3]
B = [4;5;6]

if we multiply them as follows, we get a matrix:

>> B*A
ans =
 4     8    12
 5    10    15
 6    12    18

Now my problem is: I have two 3D matrices of sizes m × n × p and m × n × q

Imagine along dimensions m and n we have pixels and for each pixel we have a vector (length p or q).
Now what I want, is to multiply for every corresponding pixel the vectors of the two images, such that for every pixel I get a matrix and thus in total a 4D Matrix in the end.

How do I do this efficiently?

  • 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-06-08T17:32:58+00:00Added an answer on June 8, 2026 at 5:32 pm

    Loops in Matlab are no longer a thing to be feared, or avoided per se.

    Granted, great care should be taken when using them, but nevertheless, the JIT can take care of many kinds of loops, improving performance even beyond builtin functions.

    Consider the following test cases:

    clc
    
    m = 512;   n = 384;
    p = 5;     q = 3;
    
    A = rand(m,n,p); % some sample data
    B = rand(m,n,q); % some sample data
    
    %% non-loop approach
    
    tic
    A2 = reshape(A,[],p);
    B2 = reshape(B,[],q);
    C2 = arrayfun(@(ii) A2(ii,:)'*B2(ii,:),1:m*n,'uni',false);
    C0 = permute(reshape(cell2mat(C2),p,q,m,n),[3 4 1 2]);
    toc
    
    %% looped approach, simplest
    
    tic
    C = zeros(m,n,p,q);
    for mm = 1:m
        for nn = 1:n        
            C(mm,nn,:,:) = ...
                squeeze(A(mm,nn,:))*squeeze(B(mm,nn,:)).';
        end
    end
    toc
    
    % check for equality
    all(C0(:) == C(:))
    
    %% looped approach, slightly optimized
    
    tic
    C = zeros(m,n,p,q);
    pp = zeros(p,1);
    qq = zeros(1,q);
    for mm = 1:m
        for nn = 1:n
            pp(:) = A(mm,nn,:);
            qq(:) = B(mm,nn,:);
            C(mm,nn,:,:) = pp*qq;
        end
    end
    toc
    
    % check for equality
    all(C0(:) == C(:))
    
    %% looped approach, optimized
    
    tic
    C  = zeros(p,q,m*n);
    A2 = reshape(A,[],p);
    B2 = reshape(B,[],q);
    for mn = 1:m*n
        C(:,:,mn) = A2(mn,:).'*B2(mn,:);
    end
    C = permute(reshape(C, p,q,m,n), [3,4,1,2]);
    toc
    
    % check for equality
    all(C0(:) == C(:))
    

    Results:

    Elapsed time is 3.955728 seconds.
    Elapsed time is 21.013715 seconds.
    ans =
         1
    Elapsed time is 1.334897 seconds.
    ans =
         1
    Elapsed time is 0.573624 seconds.
    ans =
         1
    

    Regardless of the performance, I also find the last case a lot more intuitive and readable than the non-loop case.

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

Sidebar

Related Questions

I have a very weird problem.. I really do hope someone has an answer
Hope the AWK gurus can provide a solution to my problem . I have
I have a very big problem and can't seem to find anybody else on
Hope to get solution to this problem. I have been stuck on it since
Hope someone can help me out with this problem I am having. I just
this problem has been doing my head in and I hope you can help!
I have a big problem, and I hope you can help me. I'm porting
right the problem is a smallish one but hope someone can help. Basically we
Well, I have two tables in an sql database. One has an ID column
I'm stuck with a problem and hope stackoverfolw community can help me. I want

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.