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!