Modifying the elements of matrix from indices (MATLAB)
古いコメントを表示
I have a number of elements m for example m=4. And the matrix is square of dimensions m*m
P =
1 2 3 4
2 3 4 3
3 4 3 2
4 3 2 1
using the following program to replace the indices 1, 2, 3...m with its sub-matrices:
m=input('Initialize a number of elements'); % Number of elements in the matrix
P=hankel(1:m,m:-1:1);
% Initialisation of sub-matrices of dimensions (m-1 x m-1)
a=zeros(m-1,m-1,m-2);
a(:,:,1)=eye(m-1); % First sub-matrix is always an identity matrix with indice = 1 in P
for k=2:m-1
a(:,:,k)=circshift(a(:,:,k-1),1,2 ); % Other sub-matrices of indice = 2 to m-1
end
a(:,:,m) = zeros(m-1); % Final sub-matrix of indice = m (always null)
% Replacing the sub-matrices of indices 1,2,...m in P
N = zeros(m*(m-1));
a = num2cell(a,[1 2]);
for ii = 1:m
for jj = 1:m
N((m-1)*(ii-1)+1:(m-1)*ii,(m-1)*(jj-1)+1:(m-1)*jj) = a{P(ii,jj)};
end
end
I added this part of program to find always two indices of sum=m. For example in the case of m=4 the indices are 1 and 3 to find 1+3=m
indice=0;
while sum(indice)~=m
indice=randi([1,m-1],[1,2]);
end
So the indice 2 is not used here. We use just 1, 3 and m.
I need to add a part in the prgram that replace all the sub-matrices of indices that are not used (in this example indice 2) with null matrices in the matrix N. How can I do that please.
4 件のコメント
randi() can return [2 2], so it's not true that index 2 can never be used.
m = 4;
indice = 0;
while sum(indice) ~= m
indice = randi([1,m-1],[1 2]);
end
disp(indice)
high speed
2022 年 3 月 11 日
Voss
2022 年 3 月 11 日
Are you saying that you have some code to prevent repeated indices, e.g., [2 2], or that you still need to write such code?
Also, what do you mean by null matrices? A matrix of the appropriate size which is all NaNs?
high speed
2022 年 3 月 11 日
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!