how to create 200 random matrices using loop..

3 ビュー (過去 30 日間)
Abhishek sadasivan
Abhishek sadasivan 2014 年 8 月 23 日
Hi..I want to create separate 200 random matrices of size 32*32 , and each to be multiplied with my input matrix..whether any looping is possible here ..please help ..
thank in advance

採用された回答

Image Analyst
Image Analyst 2014 年 8 月 23 日
編集済み: Image Analyst 2014 年 8 月 23 日
Try this:
tic;
inputMatrix = rand(32, 32); % Whatever you want...
for k = 1 : 200
thisRandomMatrix = rand(32,32); % Create new random matrix for this iteration.
storedMatrixes{k} = inputMatrix .* thisRandomMatrix ; % Multiply them
end
toc;
msgbox('Done with loop');
  2 件のコメント
Abhishek sadasivan
Abhishek sadasivan 2014 年 8 月 23 日
I would like to get separate 32 output matrices after multiplication so that I can take the sum of all elements in each matrix (I am trying to project my input data into random matrices ).Each sum will be one measurement ..
Image Analyst
Image Analyst 2014 年 8 月 23 日
編集済み: Image Analyst 2014 年 8 月 23 日
They are separate. Each one is in its own cell. To sum up all the elements in a particular matrix you can extract it then sum it:
thisMatrix = storedMatrixes{10}; % Get matrix #10.
theSum = sum(thisMatrix(:)); % Sum of all elements of matrix #10.
You could also store them as planes/slices in a 3D matrix if you preallocate a 3D matrix for them.

サインインしてコメントする。

その他の回答 (1 件)

Matz Johansson Bergström
Matz Johansson Bergström 2014 年 8 月 23 日
I have not tried it but something like this:
n=200;
tmp = rand(32,32,n);
and then to multiply each matrix
for i=1:n
in = in*tmp(:,:,i)
end
  2 件のコメント
Abhishek sadasivan
Abhishek sadasivan 2014 年 8 月 23 日
thanks ..but I would like to get separate 32 output matrices after multiplication so that I can take the sum of all elements in each matrix (I am trying to project my input data into random matrices ).Each sum will be one measurement ..
Matz Johansson Bergström
Matz Johansson Bergström 2014 年 8 月 23 日
編集済み: Matz Johansson Bergström 2014 年 8 月 23 日
I would argue that this is possible and very simple using a 3D matrix. The data is stored in another way, that's all and very efficient, because Matlab is made for matrices. For small matrices this does not really matter, but for larger ones it might make a difference.
My suggestion stacks the matrices into a 3d matrix and you can simply pick out the matrices like you would any other data structure.
Also, to sum all the matrices, you just have to write
sums = squeeze(sum(sum(tmp)))
This gives you the sum of all the elements in each matrix as a vector. Sums(i) contains the sum of matrix i.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by