Clear Filters
Clear Filters

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 views (last 30 days)
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

Accepted Answer

bio lim
bio lim on 30 Nov 2016
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 Comments
bio lim
bio lim on 30 Nov 2016
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

Sign in to comment.

More Answers (1)

Torsten
Torsten on 29 Nov 2016
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.

Categories

Find more on Performance and Memory in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!