how to iterate a matrix for multiple times

조회 수: 3 (최근 30일)
BOWEN LI
BOWEN LI 2019년 6월 27일
댓글: BOWEN LI 2019년 6월 27일
Hi
I have a matrix, let's say, a random 5x5 matrix. In time period 1, it is a 5x5 random matrix, in time period 2, all element in the matrix are multiplied by 2 (a number), then in time period 3, all elements in time period 2 multiplied by 2 agian, so on and so forth until time period 30.
Actually this is a small part of my research, but i just begin working on Matlab so i am so confused with this step. Thank you so much for answering this question for me!

채택된 답변

James Tursa
James Tursa 2019년 6월 27일
E.g., here is a possible outline
n = 30; % the number of iterations
M = rand(5,5); % some initial matrix
for k=1:n
M = 2*M; % or some other function involving M
end
If you need to save all the intermediate results, then something like this:
n = 30; % the number of iterations
M = zeros(5,5,n+1);
M(:,:,1) = rand(5,5); % some initial matrix
for k=2:n+1
M(:,:,k) = 2*M(:,:,k-1); % or some other function involving M(:,:,k-1)
end
  댓글 수: 1
BOWEN LI
BOWEN LI 2019년 6월 27일
Thank you so much! Your answer is very helpful.
May I ask a similar one in which the matrix are filled with binary variables, and in each iteration i want to get a new binary matrix with the same size.
The way I made the binary matrix is by using the "optimvar" that i want to use the binary matrix as my decision variable in my optimization problem. For example "y" is the binary matrix i want, and "y"=optimvar('y', 'a','b','Type','integer','LowerBound',0,'UpperBound',1)
'a' is ranging from 0-30, and 'b' is ranging from 0-30 also.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by