How to multiply a scalar with matrix within loop and for each iteration store the values in new matrix?

1 回表示 (過去 30 日間)
I have a matrix of 24x365 but just to explain what I want to do I am taking here an example matrix of 2x2.
A=[1 2
3 4]
b=0.1
n=3 (no of years)
The new matrix for each year must be formed in a way that for first year it should be simply C(1)=A but for second year the scalar b is multiplied with the C so C(2)=b*C(1).For third year it should be C(3)=b*C(2) and so on...
The answer matrix should be like
C(1)=[1 2
3 4]
C(2)= [0.1 0.2
0.3 0.4]
C(3)=[0.01 0.02
0.03 0.04]
I have formed this code but I am not getting the desired results.
clc
clear all
A=[1 2;
3 4];
b = 0.1
n=3;
c=b*a;
d=c;
for i=1:n
c(1)=d
c(i+1)=b*c(i);
end
The error appears as "Unable to perform assignment because the left and right sides have a different number of elements
Error in Untitled (line 11)
c(1)=d"
Can anyone please help me???

採用された回答

Matt J
Matt J 2020 年 5 月 18 日
編集済み: Matt J 2020 年 5 月 18 日
There is no need for a loop:
C=A.*reshape(b.^(0:n-1),1,1,[]);

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by