フィルターのクリア

For loop iteration on each element of a matrix

3 ビュー (過去 30 日間)
Syed AWM
Syed AWM 2020 年 8 月 20 日
編集済み: Bruno Luong 2020 年 8 月 21 日
I have a n-dimension matrix as the variables of an equation. I want to iterate the equation solution on each element of the variable matrix. I know about using (for 1:numel(matrix)) but I want to store the solutions of the equation on the (ith,jth) element locations in a matrix.
Thanks
  4 件のコメント
Syed AWM
Syed AWM 2020 年 8 月 21 日
I have got the code like :
x1 = [1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16];
x2 = [4 3 2 1;5 7 6 8;9 12 11 11;16 13 15 14];
for i=1:numel(x1)
f1(i)=4*x1(i)^2-x2(i)^3+28;
f2(i)=3*x1(i)^3+4*x2(i)^2-145;
end
this would give me vector [f1] and [f2] with 16 elements. While I want 4x4 matrices as solutions.
Bruno Luong
Bruno Luong 2020 年 8 月 21 日
編集済み: Bruno Luong 2020 年 8 月 21 日
Other alternative, remove completely the for-loop
x1 = [1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16];
x2 = [4 3 2 1;5 7 6 8;9 12 11 11;16 13 15 14];
f1 = 4*x1.^2-x2.^3+28;
f2 = 3*x1.^3+4*x2.^2-145;

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

採用された回答

Walter Roberson
Walter Roberson 2020 年 8 月 21 日
Preallocate the output as the appropriate size. Then use linear indexing.
f1 = zeros(size(x1)) ;

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by