Creating matrix and then using it in another code.
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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
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 件のコメント
Aniket Dutta
2022 年 8 月 24 日
What you have done is create a single matrix of 1000 x 1000 with the diagonal values of A.
what i want is 1000 different 4x4 matrix in the form of B = [A 0 0 0; 0 A 0 0; 0 0 A 0; 0 0 0 A]
basically the result should look something like
A = 1
B = [1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 1]
A = 3
B = [3 0 0 0; 0 3 0 0; 0 0 3 0; 0 0 0 3]
A = 10
B = [10 0 0 0; 0 10 0 0; 0 0 10 0; 0 0 0 10]
A = 9
B = [9 0 0 0; 0 9 0 0; 0 0 9 0; 0 0 0 9]
.
.
.
and i want the answer like this in one run.
Do you have any kind of code that could do this?
Abderrahim. B
2022 年 8 月 24 日
Do you want just to print these matrices or want them in the workspace? and is it okay to use for loop?
Aniket Dutta
2022 年 8 月 24 日
i want to use for loop
Abderrahim. B
2022 年 8 月 24 日
Check the edit!
If you want to store matrices to workspace as a struct . This struct will contain all the B matrices.
A = randi(10, 1000, 1) ;
S = struct ;
for ii = 1:length(A)
S(ii).B = diag(A(ii)*ones(4,1)) ;
end
Aniket Dutta
2022 年 8 月 24 日
Thank you.
can you also tell me how i can apply this matrix from the struct to another variable matrix to make a 6x6 matrix and create another struct in a simillar amnner
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
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 件)
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
