# 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

1 view (last 30 days)
aqib javed on 29 Nov 2016
Edited: aqib javed on 30 Nov 2016
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 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 CommentsShow 1 older commentHide 1 older comment
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
aqib javed on 30 Nov 2016
ahan, very nice explanation, i am grateful to you sir, Problem solved now, thanks again

### More Answers (1)

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.
aqib javed on 30 Nov 2016
Edited: aqib javed on 30 Nov 2016
Thanks sir for sparing time from your routine, problem solved now