Creating matrix and then using it in another code.

조회 수: 1 (최근 30일)
Aniket Dutta
Aniket Dutta 2022년 8월 24일
댓글: Aniket Dutta 2022년 8월 24일
I want to create a matrix in progression. I have 10000 random values of X in a spreadsheet. i want to impost it and then create a diagonal matrix in the form of
X 0 0 0
0 X 0 0
0 0 0 X
0 0 0 X
But for 10000 times for all 10000 different values of X in one run.
Basically I want to do Monte Carlo but with the elements of the matrix keeping the diagonal values same and getting 10000 different matrices and then run another code with of those 10000 values inone run and plot how it acts in a graph.

채택된 답변

Abderrahim. B
Abderrahim. B 2022년 8월 24일
편집: Abderrahim. B 2022년 8월 24일
Hi!
Not sure if I understood your question correctly, but here is a potential answer:
% Creating dummy data. in your case use readmatrix() to import data
A = randi(10, 1000, 1) ;
B = diag(A);
Edit:
for ii = 1:length(A)
B = diag(A(ii)*ones(4,1))
end
HTH
  댓글 수: 8
Abderrahim. B
Abderrahim. B 2022년 8월 24일
Did not get very well what you wanted to say! Below assuming that you want to create 6x6 matrices from the A,and store them in a struct :
A = randi(10, 1000, 1) ;
S_four = struct ;
S_six = struct ;
for ii = 1:length(A)
S_four(ii).B = diag(A(ii)*ones(4,1)) ;
S_six(ii).B = diag(A(ii)*ones(6,1)) ;
end
% want to access data in a field, use dot notation
B_six1 = S_six(1).B
B_six1 = 6×6
5 0 0 0 0 0 0 5 0 0 0 0 0 0 5 0 0 0 0 0 0 5 0 0 0 0 0 0 5 0 0 0 0 0 0 5
Aniket Dutta
Aniket Dutta 2022년 8월 24일
What i want to do is to make another loop to give me another set of matrices.
lets say i want to use a matrix where ii=n for which A = X
so B = [X 0 0 0; 0 X 0 0; 0 0 X 0; 0 0 0 X]
therefore my final matrix 6x6, say D should be in the form of
D = blkdiag(B(1:2,1:2), L, B(3:4,3:4), L) where say L=100.
i want this but use the values of B from the previous struct to get 10000 respective values of D.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by