Sorting and Saving a subset from the array
Mostra commenti meno recenti
Hi there,
I have imported data from Excel and now have a 240X3 cell array called 'num'.
Once sorted it looks something like this:
16 1232 56
16 3455 56
16 5443 76
16 1232 88
17 4545 65
17 3432 87
All I want to do is for to write a for loop to select all rows of the data which start with 16's and then save it as another array say num_1, then take another chunk that starts with 17's and save it as num_2..etc.
How can I compose a for loop to take care of that. I have tried different measures based on other questions here in matlab central but to no avail.
Any help will be appreciated.
Sincerely yours; Nj
Risposte (2)
Walter Roberson
il 29 Dic 2012
unum = unique(num(:,1));
for K = 1 : length(unum)
these_num = num(num == unum(K), :);
num_{K} = these_num;
end
This will create num_{1}, num_{2} and so on. Creating actual new variables is not a good idea; see http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
1 Commento
Niraj Poudel
il 29 Dic 2012
Andrei Bobrov
il 29 Dic 2012
Modificato: Andrei Bobrov
il 29 Dic 2012
a0 = {16 1232 56
16 3455 56
16 5443 76
16 1232 88
17 4545 65
17 3432 87};
a = cell2mat(a0);
[b,~,c] = unique(a(a(:,1)>=16,1));
num = ...
[num2cell(b),cellfun(@(x)a(x,2:3),accumarray(c,(1:numel(c))',[],@(x){x}),'un',0)];
Categorie
Scopri di più su Matrix Indexing in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!