# select specific values from a matrix based on indeces

1 view (last 30 days)
Ilias Minas on 4 Jun 2020
Answered: Tommy on 4 Jun 2020
Hi all,
I have a matrxi of 4*37800. I want to keep specific values based on two indeces.
The first index shows the rows that i want to keep and the second the columns every 15 steps. I mean that i split the 37800 columns to groups of 15 the index shows which column of each group i want to keep.
How can i isolate the values based on the two indeces?
I attach the folders.
iMxR is the index showing the rows that i want. ix is the index showing which colum i need. freq is the matrix.
Thank you for your help.
##### 2 CommentsShowHide 1 older comment
Ilias Minas on 4 Jun 2020
i want to start by keeping the appropriate rows using iMxR
freq(1,1)
freq(1,2)
freq(2,3) etc
It will give a single row matrix of 37800 columns, having the selected values
And using the ix, split the 37800 to groups of 15 columns and select the column that ix suggest.
Finally i will have 2520 columns and 1 row

Tommy on 4 Jun 2020
I believe this will give you the values you want:
temp = reshape(iMxR, 15, [])';
idx = sub2ind(size(temp), 1:size(temp,1), ix);
row = temp(idx); % row(i) is the row of the ith 4x15 block
col = ix; % col(i) is the column of the ith 4x15 block
temp = reshape(frequencies, 4, 15, []); % convert frequencies to a bunch of 4x15 blocks
idx = sub2ind(size(temp), row, col, 1:size(temp,3)); % convert row and col to linear indices of temp
val = temp(idx); % pull out the relevant values