Hello Everyone,
I have two long arrays that look like this:
a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]b = [10,14,3,21,21,21,21,3,14,14,14,2,75,13,13,13,100]
I need to construct a cell ( c) that will have values in array a that correspond to unique values in b. However, when b has a value that repeats (e.g. 21 above) then the entry in c should be the smallest value and the largest value in a for that value in b. That is, the result should be as follows:
c = {1,2,3,[4,7],8,[9,11],12,13,[14,16],17}
My code is too complicated and it also does not work in all cases. It looks like this:
[~,uniq_fir] = unique(b,'first');[~,uniq_las] = unique(b,'last');uniq_share = intersect(uniq_fir,uniq_las);uniq_fir_dif = setdiff(uniq_fir,uniq_las);uniq_las_dif = setdiff(uniq_las,uniq_fir);uniq1 = a(uniq_share);uniq2 = sort(a(uniq_fir_dif));uniq3 = sort(a(uniq_las_dif));c(uniq_share) = num2cell(uniq1);empt_cell_temp = find(cellfun(@isempty,c));for i = 1:length(uniq_fir_dif) c{empt_cell_temp(i)} = [uniq2(i),uniq3(i)];end
Please can anybody help me with this problem? Much obliged!
Best Answer