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 件のコメント
Guillaume
2018 年 11 月 19 日
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
2018 年 11 月 19 日
How are the max value (130.6) and the range associated?
Hajem Daham
2018 年 11 月 19 日
Hajem Daham
2018 年 11 月 19 日
Guillaume
2018 年 11 月 19 日
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
2018 年 11 月 19 日
採用された回答
その他の回答 (1 件)
madhan ravi
2018 年 11 月 19 日
編集済み: madhan ravi
2018 年 11 月 19 日
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)
カテゴリ
ヘルプ センター および File Exchange で Structures についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!