How to fill a matrix with formulas

조회 수: 24 (최근 30일)
aziz monavar
aziz monavar 2021년 9월 8일
편집: aziz monavar 2021년 9월 9일
Hi
I have a matrix as follows:
I want to fill this matrix (4,5) whose first member is y (1,1) = 0.4 using the formula
y (i) = 2 * y (i-1) * (1-y (i-1))
Please write the source for me.
  댓글 수: 2
John D'Errico
John D'Errico 2021년 9월 8일
This makes no sense at all. You have a 2 dimensional matrix. But you define
y(i) = 2 * y(i-1) * (1-y(i-1))
So a function of only ONE index.
aziz monavar
aziz monavar 2021년 9월 8일
I understand. So it is not possible to write code that uses this formula to change zero members?

댓글을 달려면 로그인하십시오.

답변 (2개)

William Rose
William Rose 2021년 9월 8일
I see from your comment that you want the transpose. Therefore do this:
y=zeros(1,20);
y(1)=0.4;
for i=2:20, y(i)=2*y(i-1)*(1-y(i-1)); end
y=reshape(y,[4,5])';
disp(y)
0.4000 0.4800 0.4992 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000
Try.
  댓글 수: 1
aziz monavar
aziz monavar 2021년 9월 8일
편집: aziz monavar 2021년 9월 9일
no, I do not mean this
I want my output matrix to be a 4x5 matrix but you have given a 5x4 matrix as output.
y=zeros(1,20);
y(1)=0.7;
for i=2:20
y(i)=1.3*y(i-1)*(1-y(i-1));
end
y=reshape(y,[4,5]);
disp(y)
0.7000 0.2430 0.2335 0.2314 0.2309 0.2730 0.2391 0.2327 0.2312 0.2309 0.2580 0.2365 0.2321 0.2311 0.2308 0.2489 0.2348 0.2317 0.2310 0.2308
My desired matrix:
y_answer =
0.7000 0.2730 0.2580 0.2489 0.2430
0.2391 0.2365 0.2348 0.2335 0.2327
0.2321 0.2317 0.2314 0.2312 0.2311
0.2310 0.2309 0.2309 0.2308 0.2308
I want to give y_answer as outputIn other words, from the first member to the fifth member of the y matrix, fill in the first row of the matrix and also the next rows of the matrix.

댓글을 달려면 로그인하십시오.


William Rose
William Rose 2021년 9월 8일
You have a matrix y(i,j) but your formula is only for y(i). That is the problem @John D'Errico identifies.
If you want to treat your 4x5 matrix as if it were 1x20, you can apply your formula, and then reshape the vector to a 4x5 matrix. The code to do it is
y=zeros(1,20);
y(1)=0.4;
for i=2:20
y(i)=2*y(i-1)*(1-y(i-1));
end
y=reshape(y,[4,5]);
disp(y)
0.4000 0.5000 0.5000 0.5000 0.5000 0.4800 0.5000 0.5000 0.5000 0.5000 0.4992 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000
Try it.
  댓글 수: 1
aziz monavar
aziz monavar 2021년 9월 8일
This is the right code for me, thank you, but it is a problem that I explained in the following example
>> y
y =
1 5 3 2 7 6 5 8 9 10 11 12 13 14 15 16
>> reshape(y,[4,4])
ans =
1 7 9 13
5 6 10 14
3 5 11 15
2 8 12 16
>> y_answer
y_answer =
1 5 3 2
7 6 5 8
9 10 11 12
13 14 15 16
"y_answer" is my answer, but "reshape" puts a column in the matrix. You can give a code that is arranged like "y_answer".

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Assembly에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by