How to create a 3D matrix from the rows a 2D matrix without using loops

Here is my problem: Initially, I have an M matrix (252x4). I need to create a W matrix (252x4x252). The restrictions are:
  • The elements in the third dimension of matrix W are 252 copies of the lines of matrix M. That is, the matrix corresponding to W(:,:, 1) is equivalent to repmat(M(1,:),[252 1]) The matrix corresponding to W(:,:,2) is equivalent to repmat (M(2,:),[252 1]) . . . The matrix corresponding to W (:,:, end) is equivalent to repmat (M (end,:), [252 1]) and so on.
  • cannot use for loop or similar.
Is there any way to do linear transformations or using the repmat function to compose the W matrix without using loop?

2 Comments

What have you tried so far? Does your homework assignment specifically forbid any other functions? Or just forbids loops?
Well...it only forbid to use loops.
At this point i've managed to use just one loop. M is the 252x4
What I would really like to do is eliminate the loop and compose the final K matrix. As you can see, I chose not to use repmat on the line of code that contains' sub = ones (M, 1) * X (i , :) - X; ' However, it was not an imposed restriction.
hyper = 0.5;
[R,L]=size(M);
K=zeros(R);
den=2*hyper*hyper;
for i=1:R,
sub = ones(R,1)*M(i,:) - M;
K(i,:) = exp(diag(-sub*sub')'./den);
end

Sign in to comment.

 Accepted Answer

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 28 Jan 2021

Commented:

on 29 Jan 2021

Community Treasure Hunt

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

Start Hunting!