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 件のコメント

Dyuman Joshi
Dyuman Joshi 2021 年 5 月 1 日
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
Waseem Ahmad 2021 年 5 月 1 日
Dear Joshi, now i understood the cause of the error. Can you please help me with how to initiate the algorithm. As im a beginner and don't know much about metrics algorithms. Thank you
Walter Roberson
Walter Roberson 2021 年 5 月 1 日
Each of your A variables is a 3 by 3 array. How big are you expecting each of your Y to be?
Waseem Ahmad
Waseem Ahmad 2021 年 5 月 1 日
Dear Roberson, I want Y to be 3by3
Image Analyst
Image Analyst 2021 年 5 月 1 日
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
Waseem Ahmad 2021 年 5 月 1 日
Thank you Image Analyst your code work for me

サインインしてコメントする。

 採用された回答

Jan
Jan 2021 年 5 月 1 日
編集済み: Jan 2021 年 5 月 1 日

0 投票

Either collect all Y in a 3D array:
N = 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];
Y = zeros(3, 3, N);
for i = 1:N
% Original:
% 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))))
% Simplified 1:
Y(:, :, i+1) = 0.5 * ((0.25 * (Y(:, :, i) + A1)) + ...
0.25 * (Y(:, :, i) + A1)) + ...
0.25 * (((1 / 6) * (Y(:, :, i) + A2)) + ...
(1 / 6) * (Y(:, :, i) + A2)) + ...
1/6 * (((1 / 8) * (Y(:, :, i) + A3)) + ...
((1 / 8) * (Y(:, :, i) + A3)));
% Simplified 2:
% Y(:, :, i+1) = 1 / 4 * (Y(:, :, i) + A1) + ...
% 1 / 12 * (Y(:, :, i) + A2) + ...
% 1 / 24 * (Y(:, :, i) + A3);
end
I've simplified the code, because then it is much easier to modify and to debug.
Or omit the indexing for Y:
N = 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];
Y = zeros(3, 3);
for i = 1:N
Y = (Y + A1) / 4 + ...
(Y + A2) / 12 + ...
(Y + A3) / 24;
end

その他の回答 (1 件)

Image Analyst
Image Analyst 2021 年 5 月 1 日

0 投票

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 件のコメント

Jan
Jan 2021 年 5 月 1 日
It's interesting, how differently we understood the question.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMatrices and Arrays についてさらに検索

質問済み:

2021 年 5 月 1 日

コメント済み:

Jan
2021 年 5 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by