How Can I Make a Matlab Code generate multiple separate matrices
古いコメントを表示
I want to generate 21 matrices from a code that are 4x4 with values in place that I can set elsewhere in the code. I don't want to type out 21 different 4x4 matrices.
回答 (2 件)
Jos (10584)
2015 年 3 月 30 日
編集済み: Jos (10584)
2015 年 3 月 30 日
To generate one 4-by-4 matrix with a specific value you can use various approaches
Value = 3 ;
A = repmat(Value,4,4)
B = Value * ones(4,4)
C = zeros(4,4), C(:) = Value
You have to think about how to store several of these matrices. As planes in 3D arrays or, for instance, as cells in a cell array? The first option can only be used when all 2D matrices have the same size
Values = [3 6 99] ;
N = numel(Values)
Array3D = zeros(4,4,N) % a 4-by-4-by-3 3D array
for k=1:N
Array3D(:,:,k) = Values(k)
end
or in a cell array
Values = [7 -3 5 4]
CellArray = arrayfun(@(x) repmat(x,4,4),Values,'un',0)
disp(CellArray{1})
7 件のコメント
Declan Simons
2015 年 3 月 30 日
Jos (10584)
2015 年 3 月 30 日
an XY problem ... :(
"I need to store each of the matrices created as a different variable" is very likely untrue. Instead of telling us what you think you need to do, why not tell us what calculations you actually need to perform on this data and we can give some advice about the easiest ways to do this.
Declan Simons
2015 年 3 月 30 日
This seems like a perfect opportunity to use a cell array to store the 21 matrices in. This would allow you to generate them in a loop, and access them use any of the many tools for working with cell arrays, such as cell array indexing.
So far you have not written any details about how these matrices are to be generated, nor about exactly how these values are to be used. This means we have given general advice, but without more specific information we cannot help much more.
Declan Simons
2015 年 3 月 30 日
out = cell(1,21);
for trial = 1:21
out{trial} = ... your calculation here
end
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!