Randperm in symmetric matrix
2 views (last 30 days)
If i have a symmetric matrix and i want to select a random entries from this matrix and let these random entries belong to a set omega.. I can do that by use a randperm function however since the matrix is symmetric, I want to select the same corresponding entries.. so for example if we have 4×4 matrix and in randperm select the indices 2 6 8 and set this as omega then i want to update omega to include also the symmetric position such that 5 6 14
More Answers (2)
Jack on 3 Apr 2023
To select random entries from a symmetric matrix and ensure that the corresponding symmetric positions are also included in the selected set, you can do the following:
- Use the randperm function to generate a random set of indices omega in the lower triangle of the matrix.
- Generate the corresponding indices in the upper triangle by adding the number of columns to each index. For example, if omega = [2 6 8], then the corresponding upper triangle indices would be omega + 4 = [6 10 12].
- Concatenate the two sets of indices to obtain the complete set of indices omega, including both the lower and upper triangle.
Here's an example code snippet that implements these steps:
% Generate a random symmetric matrix
n = 4;
A = randn(n);
A = A + A.'; % make A symmetric
% Generate a random set of indices in the lower triangle
n_lower = n*(n-1)/2;
omega_lower = randperm(n_lower, 3);
% Generate the corresponding indices in the upper triangle
omega_upper = omega_lower + n;
% Concatenate the two sets of indices to obtain the complete set
omega = sort([omega_lower omega_upper]);
% Display the selected indices and their corresponding symmetric positions
fprintf('Corresponding symmetric positions:\n');
disp(omega(:) + n*(ceil(omega(:)/n) - 1) - (n*(n-1)/2)));
This code generates a random 4x4 symmetric matrix A and selects a random set of 3 indices in the lower triangle using randperm. It then computes the corresponding indices in the upper triangle and concatenates the two sets of indices to obtain the complete set omega. Finally, it displays the selected indices and their corresponding symmetric positions in the matrix.
Note that in the last line of the code, the symmetric positions are computed using a formula that maps each index to its corresponding row and column in the lower triangle of the matrix, and then maps that position to its corresponding index in the upper triangle. This formula assumes that the matrix is square (i.e., n is even) and that the indices in omega are sorted in ascending order. If your matrix has a different size or the indices are not sorted, you may need to adjust the formula accordingly.
Walter Roberson on 3 Apr 2023
Moved: Walter Roberson on 3 Apr 2023
You can build a mapping:
LINEARINDICES = randperm(16,3)
MAPPING = reshape(reshape(1:16,4,4).', 1, );
SYMLINEARINDICES = MAPPING(LINEARINDICES)
[r, c] = ind2sub([4 4], LINEARINDICES);
SYMLINEARINDICES2 = sub2ind([4 4], c, r)