How can I make a variant submatrix?

1 回表示 (過去 30 日間)
Felipe Ribas
Felipe Ribas 2020 年 10 月 22 日
コメント済み: Vijay 2020 年 10 月 22 日
Hello everybody,
Does anyone know how I can make a variant submatrix, for example, if I have a matrix A (100,100) use a submatrix B (10,10) to get the first 10 rows and 10 columns, then the next 10 columns so on until all elements of the columns and rows have been taken.

採用された回答

Vijay
Vijay 2020 年 10 月 22 日
You could try below
(1) If you are just aiming to the get the submatrices in the diagonal of A...
I = length(A);
n = 10; % size of the desired squared submatrix
i= I/n;
% s1 = 'B';
% s2 =[1:i];
for j=1:i
% s3=num2str(s2(j));
eval(sprintf('B%d = A((j-1)*n+1:n*j,(j-1)*n+1:n*j)', j));
end
(2) If you are aiming to cover all the rows and columns...
I = length(A);
n = 10; % size of the desired squared submatrix
i= I/n;
for j=1:i
for k=1:i
eval(sprintf('B%d = A((j-1)*n+1:n*j,(k-1)*n+1:n*k)', (j-1)*i+k));
end
end
  1 件のコメント
Vijay
Vijay 2020 年 10 月 22 日
*Note: This code works only if your reference matrix is a square matrix

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

その他の回答 (1 件)

Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam 2020 年 10 月 22 日
You can use vectors to point to the indices of matrix A,
For example:
A(1:10, 1:10); % the first one
A(11:20, 1:10) % second submatrix
A(21:30, 1:10)
%...
A(91:100, 1:10);
A(1:10, 11:20 ...
% ...
A(91:100, 91:100);
% you need to have a loop to go through all sub matrices

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by