How to split array vector into sub vectors based on the max value of the original array?

I have this row vector which can contains fractional or discrete integer values:
a = [ 120.9, 50.5, 20.3, 80.7, 12 , 70, 100, 15.4, 40, 10, 130.6, 25.3]
based on the max value of a, which is in this case 130.6, I want to split this array into three or (sometimes less/more) sub arrays in ascending( starting from small to max value) based on specific rang.
The number of elements are not equel for each sub array, so the output sub arrays could be like these:
a1 = [10, 12, 15.4]
a2 = [20.3, 25.3, 40, 50.5, 70]
a3 = [80.7, 100, 120.9, 130.6]

6 Kommentare

Don't create numbered variables. Use a cell array that you index instead, so a{1}, a{2}, a{3}, etc.
What's really not clear from your question is the rule that defines how to split the vector.
How are the max value (130.6) and the range associated?
The most importatnt thing is to ensure that theres no empty sub arrays when doing the splitting. So there is no specific rang.
ignore this max value because it can be changed always, but the difficulity is that the max value can be fractional like this example or sometimes discrete
You need to explain clearly what the rule is for splitting the array. Why does a1 gets the first 3 elements. Why does a2 gets the next 4? etc.
Doing what you want is extremely easy, whatever the rule is. We have no idea what the rule is, so the only thing we can tell you so far is to use mat2cell as per madan's answer. If you tell us the rules, then we can tell you what to pass to mat2cell.
Ok, the two important rules are firstly to ensure that there are no empty sub arrays and secondly to balance as possible the number of elemnts at each sub array.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

a = [ 120.9, 50.5, 20.3, 80.7, 12 , 70, 100, 15.4, 40, 10, 130.6, 25.3];
n = 3;
maxa = max(a);
result = cell(1, n);
mask = true(size(a));
for k = 1:n
match = (a <= maxa * (k/n));
result{k} = sort(a(match & mask));
mask(match) = false;
end
This does not return the example data given in the question:
a1 = [10, 12, 15.4]
a2 = [20.3, 25.3, 40, 50.5, 70]
a3 = [80.7, 100, 120.9, 130.6]
but
a1 = [10, 12, 15.4, 20.3, 25.3, 40]
a2 = [50.5, 70, 80.7]
a3 = [100, 120.9, 130.6]
Therefore I'm not sure if this is really what you are asking for.

Weitere Antworten (1)

madhan ravi
madhan ravi am 19 Nov. 2018
Bearbeitet: madhan ravi am 19 Nov. 2018
This would give the combination of splits that can be expected:
a = [ 120.9, 50.5, 20.3, 80.7, 12 , 70, 100, 15.4, 40, 10, 130.6, 25.3]
while 1
p=randperm(5,3); % you can change number 5 to any number you want and 3 also which defines the number of splits that you expect
%or
p=randi([1 5],1,3); %if you want the splits to contain similar number of elements
if sum(p)==numel(a)
P=p;
break
else continue
end
end
a = mat2cell(sort(a),1,P)
celldisp(a)

Kategorien

Gefragt:

am 19 Nov. 2018

Kommentiert:

am 19 Nov. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by