# Array Exponentials without For Loop?

2 ビュー (過去 30 日間)
Alec Huynh 2021 年 6 月 17 日
コメント済み: Alec Huynh 2021 年 6 月 25 日
Hello, I hope you are doing well! I am not entirely sure how to succinctly describe the question I have, so I will instead demonstrate it.
Let's say I have two arrays:
A = [1 2 3];
B = [2 3];
I would like to do the operation A^B such that I would receive a 2D array C containing the solution of A.^B(1) and A.^B(2).
C(1,:) = A.^B(1);
C(2,:) = A.^B(2);
C = [1 4 9; 1 8 27];
I understand that I could write a For loop like this:
% Full Loop Code
A = [1 2 3];
B = [2 3];
C = zeros(length(B), length(A));
for i = 1: length(B)
C(i, :) = A.^B(i);
end
But I am interested in knowing how to do this using only vectors and not using a For loop. When I do:
C = A.^B
Error using .^
Matrix dimensions must agree.
Any suggestions? I am using MATLAB 2016b.
Thank you!

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

### 採用された回答

Jan 2021 年 6 月 17 日
A = [1 2 3];
B = [2 3];
C = A .^ (B.')
##### 4 件のコメント表示非表示 3 件の古いコメント
Alec Huynh 2021 年 6 月 25 日
@Steven Lord, @Jan Thank you for the succinct explanations!

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

R2016b

### Community Treasure Hunt

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

Start Hunting!