Algorithm for the matrices
Afficher commentaires plus anciens
Hello dears,
I am a beginner and want to use the algorithm
Y(i+1)=((1/2).*(1/4)*(Y(i))+2*A1)+((1/4).*(1/6)*(Y(i))+2*A2)+((1/6).*(1/8)*(Y(i))+2*A3);
where
A1=[4/7 7/8 0; 7/8 4/7 0; 0 0 0], A2=[3/4 1 0; 1 3/4 0; 0 0 0], A3=[1/8 1 0; 1 1/8 0; 0 0 0]
and initial values for Y are zeros.
My code is
N=10;
nstep=0:10;
Y=zeros(3,3);
A1=[4/7 7/4 0; 7/4 4/7 0; 0 0 0];
A2=[3/4 1 0; 1 3/4 0; 0 0 0];
A3=[1/8 1 0; 1 1/8 0; 0 0 0];
for i=1:N
Y(i+1)=(1./2).*(((1./4).*(Y(i)+A1))+(1./4).*(Y(i)+A1))+(1./4).*(((1./6).*(Y(i)+A2))+(1./6).*(Y(i)+A2))+((1./6).*(((1./8).*(Y(i)+A3))+((1./8).*(Y(i)+A3))))
end
But the code gives the following error
"In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in Code (line 8)
Y(i+1)=(1./2).*(((1./4)*(Y(i)+A1))+(1./4).*(Y(i)+A1))+(1./4).*(((1./6)*(Y(i)+A2))+(1./6).*(Y(i)+A2))+(1./6).*(((1./8)*(Y(i)+A3))+(1./8).*(Y(i)+A3)); "
I don't know how to proceed. Any help will be appreciated. Thank you all in advance for your valuable time.
6 commentaires
Dyuman Joshi
le 1 Mai 2021
You are assigning an array as an element of another matrix, which is simply not possible.
"In an assignment A(I) = B, the number of elements in B and I must be the same"
This error is encountered when you try to place more elements into A that are specified by the I variable. For example if A is a simple vector, and I is a simple index, like 3, then trying to put 100 numbers into A(3) will obviously not work.
Waseem Ahmad
le 1 Mai 2021
Walter Roberson
le 1 Mai 2021
Each of your A variables is a 3 by 3 array. How big are you expecting each of your Y to be?
Waseem Ahmad
le 1 Mai 2021
Image Analyst
le 1 Mai 2021
Then why is N 10? If you set it to 3, does this give you what you want:
N=3;
nstep=0:10;
A1=[4/7 7/4 0; 7/4 4/7 0; 0 0 0];
A2=[3/4 1 0; 1 3/4 0; 0 0 0];
A3=[1/8 1 0; 1 1/8 0; 0 0 0];
[rows, columns] = size(A1)
Y = zeros(rows, N);
% Loop over all rows in the A's:
for row = 1 : rows
for i = 1 : N - 1
term1 = (1/2).*(((1/4).*(Y(row, i)+A1(row,:)))+ (1/4).*(Y(row, i)+A1(row,:)))
term2 = (1/4).*(((1/6).*(Y(row, i)+A2(row,:)))+ (1/6).*(Y(row, i)+A2(row,:)))
term3 = ((1/6).*(((1/8).*(Y(row, i)+A3(row,:)))+ ((1/8).*(Y(row, i)+A3(row,:)))))
Y(row, 1:length(term1)) = term1 + term2 + term3
end
end
Waseem Ahmad
le 1 Mai 2021
Réponse acceptée
Plus de réponses (1)
Image Analyst
le 1 Mai 2021
N=3;
nstep=0:10;
A1=[4/7 7/4 0; 7/4 4/7 0; 0 0 0];
A2=[3/4 1 0; 1 3/4 0; 0 0 0];
A3=[1/8 1 0; 1 1/8 0; 0 0 0];
[rows, columns] = size(A1)
Y = zeros(rows, N);
% Loop over all rows in the A's:
for row = 1 : rows
for i = 1 : N - 1
term1 = (1/2).*(((1/4).*(Y(row, i)+A1(row,:)))+ (1/4).*(Y(row, i)+A1(row,:)))
term2 = (1/4).*(((1/6).*(Y(row, i)+A2(row,:)))+ (1/6).*(Y(row, i)+A2(row,:)))
term3 = ((1/6).*(((1/8).*(Y(row, i)+A3(row,:)))+ ((1/8).*(Y(row, i)+A3(row,:)))))
Y(row, 1:length(term1)) = term1 + term2 + term3
end
end
1 commentaire
Jan
le 1 Mai 2021
It's interesting, how differently we understood the question.
Catégories
En savoir plus sur Matrices and Arrays dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!