How to split array vector into sub vectors based on the max value of the original array?
Ältere Kommentare anzeigen
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
Guillaume
am 19 Nov. 2018
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.
ES
am 19 Nov. 2018
How are the max value (130.6) and the range associated?
Hajem Daham
am 19 Nov. 2018
Hajem Daham
am 19 Nov. 2018
Guillaume
am 19 Nov. 2018
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.
Hajem Daham
am 19 Nov. 2018
Akzeptierte Antwort
Weitere Antworten (1)
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)
1 Kommentar
Hajem Daham
am 19 Nov. 2018
Kategorien
Mehr zu Structures finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!