Matrix calculation and powers

11 ビュー (過去 30 日間)
MattH
MattH 2018 年 4 月 28 日
コメント済み: Ameer Hamza 2018 年 4 月 28 日
Hello, Is there a way to elevate each element of a 2D matrix to several powers at once (without using a loop) ? The result should be a 3D matrix where each "layer" corresponds to the initial data matrix elevated to a certain power.
rawValues=[7 3 7 9 4 2 5 5;5 2 3 10 2 2 4 3;2 6 7 7 8 10 6 5];
nRow=size(rawValues,1);
powerMatrix = repmat(1:5,nRow,1);
powerVector=1:5;
d3Matrix= zeros(nRow,8,5);
d3Matrix(:,:,1)=rawValues.^powerVector(:,1);
I tried different ways but I always got a dimension error message. I am not sure whether the powers should be in a matrix or a vector (nothing has worked so far...). I am sure it is just a stupid matter of syntax...
Any idea ?
Many thanks !
  1 件のコメント
MattH
MattH 2018 年 4 月 28 日
forget the zeros()

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

採用された回答

Ameer Hamza
Ameer Hamza 2018 年 4 月 28 日
You can do it in one line, just make the dimensions appropriate.
rawValues=[7 3 7 9 4 2 5 5;5 2 3 10 2 2 4 3;2 6 7 7 8 10 6 5];
powerMatrix = reshape(1:5, 1, 1, []);
Result = rawValues.^powerMatrix;
  2 件のコメント
MattH
MattH 2018 年 4 月 28 日
Great, thanks you ! I knew it was possible !
Ameer Hamza
Ameer Hamza 2018 年 4 月 28 日
You are welcome.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by