How can i repeat a formula without for loop?

5 ビュー (過去 30 日間)
Volkan Yangin
Volkan Yangin 2021 年 7 月 8 日
回答済み: G A 2021 年 7 月 8 日
Hi,
I want to repeat my formula according to p value. For this purpose, for loop can be easily used, but i should not implement a loop for this work and unfortunately didn't find an effective solution for this.
Assume that A and C are matrices like
A = [ 1 2; 3 4]
C = [5 6; 7 8]
B matrix is:
B = [CA CA^2 CA^3 ... CA^p]' (p may be equal to any number)
Is there any way to run this without any loop?
Thanks,
EDIT: Performing of this work by using for loop:
clear all
clc
i = [1:10]
A = [1 2 ; 3 4]
C = [ 5 6 ; 7 8]
for i = 1:10
B{i} = [C*A^i]
end
B = transpose(cell2mat(B))
  2 件のコメント
Jonas
Jonas 2021 年 7 月 8 日
if you mean C*(A^p) you can improve the code by
B=cell(1,10);
B{1}=C*A;
for p=2:10
B{i}=B{i-1}*A;
end
Volkan Yangin
Volkan Yangin 2021 年 7 月 8 日
Thanks, but i struggle to make same thing without using any loop.

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

採用された回答

G A
G A 2021 年 7 月 8 日
M = [1,2; 3,4];
LM=log(M);
A = 1:5;
B = num2cell(A);
C = cellfun(@(x) {x*LM}, B);
D = cellfun(@(x) {exp(x)},C);
D{:}

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by