Creating 3D matrix from a vector with special order of elements without using a loop command

Imagine a vector P of the form P=[ p 1, p 2, p 3, ... p k] where k is the number of elements in vector P. I would like to create an i X j X k dimensional matrix Q where Q(i,j,1)= p 1, Q(i,j,2)= p 2, ..., Q(i,j,k)= p k. Naturally this would be easy using a loop command but I was wondering if it is possible to do this using only matrix manipulation possibilities within Matlab.

2 件のコメント

Stephen23
Stephen23 2016 年 1 月 26 日
編集済み: Stephen23 2016 年 1 月 26 日
What value/s does Q have for the rows and columns not equal to i and j ?
Saeid
Saeid 2016 年 1 月 27 日
Basically, this is the way I want the product of this operation to look.

サインインしてコメントする。

 採用された回答

Stephen23
Stephen23 2016 年 1 月 26 日
編集済み: Stephen23 2016 年 1 月 27 日
>> R = 2; %rows
>> C = 3; %columns
>> P = 1:6;
>> Q(R,C,:) = P;
>> size(Q)
ans =
2 3 6
This places the vector P along the third dimension (page) on row 2, column 3. The other values will be automatically filled with zeros. If instead you want the vector P replicated (without any zeros), then try this:
>> Q = repmat(reshape(P,1,1,[]),R,C);
>> size(Q)
ans =
2 3 6

3 件のコメント

Saeid
Saeid 2016 年 1 月 27 日
That's EXACTLY what I wanted! Thank you very much Stephen. You have no idea how much time I spent trying to do this! Have a great day!
Stephen23
Stephen23 2016 年 1 月 27 日
編集済み: Stephen23 2016 年 1 月 27 日
My pleasure! You can also accept the answer that best resolves your question.
Saeid
Saeid 2016 年 1 月 30 日
Just did, sorry, I'm new to the community!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

2016 年 1 月 26 日

コメント済み:

2016 年 1 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by