concatenating / manipulating matrix based on user input

1 次查看(过去 30 天)
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
Sven 2011-11-12
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 个评论
karan
karan 2011-11-12
PERFECT!!!!!
i tried the for-loop structure before but it wasn't storing the data for the 'n' number of inputs ...all it did was give me the last sector of data that i wanted...
But your code above works...
I really appreciate your help...
Andrei Bobrov
Andrei Bobrov 2011-11-12
cell2mat(arrayfun(@(x)matrix_dates(x,1):matrix_dates(x,2),ind_to_calc,'un',0)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by