Azzera filtri
Azzera filtri

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 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

bio lim
bio lim il 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 Commenti
bio lim
bio lim il 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
aqib javed il 30 Nov 2016
ahan, very nice explanation, i am grateful to you sir, Problem solved now, thanks again

Accedi per commentare.

Più risposte (1)

Torsten
Torsten il 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.

Categorie

Scopri di più su Performance and Memory in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by