# concatenating / manipulating matrix based on user input

1 view (last 30 days)
karan on 12 Nov 2011
So below is the matrix which gives me indexes of data sets defined by dates(represented in column 3, column 1 and column 2 are the first index and last index of that data set).
Matrix_dates_present_indexes =
1 1701 1
1702 4955 2
4956 8286 3
8287 11458 4
11459 14740 5
14741 18019 6
18020 21522 7
21523 24994 8
24995 27057 9
I made the above table so i can grab these indexes and get the represeting values.
what i want to achieve is a extracted matrix based on a user input lets say the user wants data analysis of section 1,2,7,8 only from the whole data set.
index_for_calculation = [1, 2, 7, 8]
then how can u get data into a new matrix with concatenated data saying
new_matrix_want = [1:1701 , 1702:4955 , 18020:21522 ,21523:24994 ]
i hope u understand my question...if not i would like to give more calculation..
thanks

Sven on 12 Nov 2011
Hi Karan, try this:
The setup:
matrix_dates = [ 1 1701 1
1702 4955 2
4956 8286 3
8287 11458 4
11459 14740 5
14741 18019 6
18020 21522 7
21523 24994 8
24995 27057 9]
inds_to_calc = [1, 2, 7, 8];
The "for-loop" way:
indices_cell = cell(size(inds_to_calc));
for i = 1:length(inds_to_calc)
from = matrix_dates(inds_to_calc(i),1);
to = matrix_dates(inds_to_calc(i),2);
indices_cell{i} = from:to;
end
all_indices = cat(2, indices_cell{:});
Or you could try the sneaky (more difficult to follow) 1-line way:
all_indices = cell2mat(arrayfun(@(from,to)from:to, matrix_dates(inds_to_calc,1), matrix_dates(inds_to_calc,2), 'UniformOutput',false)');
Either way, the answer that you wanted:
new_matrix_want = [1:1701 , 1702:4955 , 18020:21522 ,21523:24994 ]
is in the "all_indices" variable.
##### 2 CommentsShowHide 1 older comment
Andrei Bobrov on 12 Nov 2011
cell2mat(arrayfun(@(x)matrix_dates(x,1):matrix_dates(x,2),ind_to_calc,'un',0)