Select a given number of elements from two vectors without overlap given elements importance

1 visualización (últimos 30 días)
Hello,
I have two vectors of two conditions (F and S), each containing voxel IDs ordered from most to least important voxel (sorted in descend order), e.g. F = [18793; 17720; 17721; 17696 ...]. The goal is to generate an ROI mask that has the most important voxels from both F and S conditions, such that we add the most important voxel from F and S, followed by the second most important voxel, third important voxel, and so on. But the trick is here: If the next most important voxel in one condition was already selected in the ROI from the other condtion, then we skip this voxel and select the next important voxel that wasn't selected in the ROI (so far). The final number of voxels in the ROI is set by a threshold (threshold = 5000), 2500 must be selected from F and 2500 from S.
Here's an example:
Take first voxel ID from F, and alternate between the F and S: (bolded elements are selected in the final ROI)
F = [18793;17720;17721;17696;18794;17697;18818;17719;17737 ... ]
S = [18793;17721;18794;17720;17722;17697;17698;18818;17696 ... ]
ROI = [ 18793; 17721; 17720; 18794; 17696; 17722; 17697; 17698; 18818; ];
Notice how S(1), S(4) and S(6) weren't selected because they were already selected from F. Same is true for F(3) and F(5).
And we you keep selecting voxels until you select threshold/2 voxels from F and S (both conditions contribute equally to the final ROI).
Note that at the end we don't care about keeping the voxel IDs order in the same way I manually generated above in final ROI. What we care about is really getting the correct IDs in the ROI to generate a binary mask, and so we need to select the IDs correctly as I described above.
I don't want to do this element by element because it takes so much time. Is there a way to vectorize this process?
Thank you for your help in advance!

Respuestas (1)

Deepak
Deepak el 9 de Dic. de 2024
To efficiently create an ROI mask from two voxel lists (F and S) without duplicates and with equal contributions, we use logical indexing in MATLAB.
We initialize a logical array to track selected voxel IDs, allowing us to alternate between F and S, adding unique voxel IDs to the ROI. This ensures that half of the required voxels are selected from each condition. By leveraging logical operations, we avoid nested loops, enhancing performance and ensuring the final selection meets the criteria.
Below is a sample MATLAB code to achieve the same:
% Define the vectors F and S, and the threshold
F = [18793; 17720; 17721; 17696; 18794; 17697; 18818; 17719; 17737];
S = [18793; 17721; 18794; 17720; 17722; 17697; 17698; 18818; 17696];
threshold = 5000;
half_threshold = threshold / 2;
% Initialize variables
roi = [];
selected_voxels = false(max(max(F), max(S)), 1);
f_count = 0;
s_count = 0;
% Initialize indices for F and S
f_idx = 1;
s_idx = 1;
% Loop until we have selected enough voxels from both F and S
while f_count < half_threshold || s_count < half_threshold
if f_count < half_threshold
% Select the next unique voxel from F
while f_idx <= length(F) && selected_voxels(F(f_idx))
f_idx = f_idx + 1;
end
if f_idx <= length(F)
roi = [roi; F(f_idx)];
selected_voxels(F(f_idx)) = true;
f_count = f_count + 1;
f_idx = f_idx + 1;
end
end
if s_count < half_threshold
% Select the next unique voxel from S
while s_idx <= length(S) && selected_voxels(S(s_idx))
s_idx = s_idx + 1;
end
if s_idx <= length(S)
roi = [roi; S(s_idx)];
selected_voxels(S(s_idx)) = true;
s_count = s_count + 1;
s_idx = s_idx + 1;
end
end
end
disp('Selected ROI voxels:');
disp(roi);
Please find attached the documentation of functions used for reference:
I hope this will help in resolving the issue.

Categorías

Más información sobre Convert Image Type en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by