×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

3-D dot product?

3-D dot product?

3-D dot product?

(OP)
I'd like to multiply a 1-D vector by a 2-D array without using a loop.  Here's what I currently have:

A = ones(10,10);
z = 1:10;
for i = 1:length(z)
   result(:,:,i) = z(i)*A;
end

Is there any way to do some sort of dot product between z and A that achieves the same result?

Thanks

RE: 3-D dot product?

dot product on the rows of A:

CODE

z.*A(i,:)

dot product on the columns of A:

CODE

z.*A(:,i)

If you want to keep z as a row/column vectorI think you will still need to loop, Although you could make a temporary variable that was padded with lots of z's...

CODE

ztemp=zeros(size(A))
ztemp(1:size(A,1),:) = z
result = z.*A

HTH



RE: 3-D dot product?

Here's the trick.
1. Make a 3d matrix where each page is a copy of the 2d matrix
2. Make a 3d matrix where each page is a 2d matrix filled with each value in z
3. Use ".*" to compute the product

This should be considerably quicker than the for loop for large z. Make it into a function if you are using it a lot.

CODE

% define the two operands
A = rand(10,9);
z = [1:10];
n = length(z);
[A_rows, A_cols] = size(A);
% make a 3d version of z so that the values of z are along the 3rd dimension
z_3d = zeros(1,1,n);
z_3d(1,1,:) = z;
z_all = repmat(z_3d,[A_rows, A_cols, 1]);
% Make n copies of A in a 3d array;
A_all = repmat(A,1,1,n);
% compute the product
answer = A_all.*z_all;
M

--
Dr Michael F Platten

RE: 3-D dot product?

(OP)
Good idea, Dr. Mike.  Thanks for the help.  Saves me MASSIVE computing time.  Seems like the Mathworks guys would just make something like this a built-in function for us dummies.  ;)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources