i am getting an error while executing a short code to update a matrix, In an assignment A(I) = B, the number of elements in B and I must be the same. Er

2 ビュー (過去 30 日間)
A = [0 1 1 1 ;0 0 0 1 ;0 1 0 1 ;0 0 0 0];
N=4; C=2;
rho=0.1;
B= zeros(N,C);
a=[1 0;0 1;1 1;0 1]
for k=1:N-1
B(k+1) = B(k) + (0.5 * rho* a);
end

採用された回答

bio lim
bio lim 2016 年 11 月 30 日
Not sure if this is what you are trying to do, but I am taking Torsten's approach.
A=[0 1 1 1 ;0 0 0 1 ;0 1 0 1 ;0 0 0 0];
N=4;
C=2;
rho=0.1;
B=zeros(N,C,N);
a=[1 0;0 1;1 1;0 1];
for k=1:N-1
B(:,:,k+1) = B(:,:,k) + (0.5 * rho* a);
end
  3 件のコメント
bio lim
bio lim 2016 年 11 月 30 日
You are making mistakes with indices. Think about the operation you are trying to do here.
B(k+1) = B(k) + (0.5 * a);
a is a matrix, but B(k) gives you a single element of your B matrix. Hence; this operation will give you an error. What you can do is make B a cell matrix.
Aside from that, B is getting updated in the top code.
B(:,:,1) =
0 0
0 0
0 0
0 0
B(:,:,2) =
0.05 0
0 0.05
0.05 0.05
0 0.05
B(:,:,3) =
0.1 0
0 0.1
0.1 0.1
0 0.1
B(:,:,4) =
0.15 0
0 0.15
0.15 0.15
0 0.15
aqib javed
aqib javed 2016 年 11 月 30 日
ahan, very nice explanation, i am grateful to you sir, Problem solved now, thanks again

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

その他の回答 (1 件)

Torsten
Torsten 2016 年 11 月 29 日
You mean
A=[0 1 1 1 ;0 0 0 1 ;0 1 0 1 ;0 0 0 0];
N=4;
C=2;
rho=0.1;
B=zeros(N,N,C);
a=[1 0;0 1;1 1;0 1]
for k=1:N-1
B(k+1,:,:) = B(k,:,:) + (0.5 * rho* a);
end
?
Best wishes
Torsten.
  1 件のコメント
aqib javed
aqib javed 2016 年 11 月 30 日
編集済み: aqib javed 2016 年 11 月 30 日
Thanks sir for sparing time from your routine, problem solved now

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

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by