How to fill a matrix with formulas

25 views (last 30 days)
aziz monavar
aziz monavar on 8 Sep 2021
Edited: aziz monavar on 9 Sep 2021
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 Comments
John D'Errico
John D'Errico on 8 Sep 2021
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 on 8 Sep 2021
I understand. So it is not possible to write code that uses this formula to change zero members?

Sign in to comment.

Answers (2)

William Rose
William Rose on 8 Sep 2021
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 Comment
aziz monavar
aziz monavar on 8 Sep 2021
Edited: aziz monavar on 9 Sep 2021
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.

Sign in to comment.


William Rose
William Rose on 8 Sep 2021
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 Comment
aziz monavar
aziz monavar on 8 Sep 2021
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".

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!