Effacer les filtres
Effacer les filtres

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 vues (au cours des 30 derniers jours)
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

Réponse acceptée

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

Connectez-vous pour commenter.

Plus de réponses (1)

Torsten
Torsten le 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.
  1 commentaire
aqib javed
aqib javed le 30 Nov 2016
Modifié(e) : aqib javed le 30 Nov 2016
Thanks sir for sparing time from your routine, problem solved now

Connectez-vous pour commenter.

Catégories

En savoir plus sur Performance and Memory dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by