MATLAB: Concatenating / manipulating matrix based on user input

concatenationmatrix manipulationuser input

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

Best Answer

  • 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.