I have a matrix with positive and negative values as:
a=[0 1;-3 3;-2 -7;0 5;4 -6;1 -2].
I would like to find by columns the maximum positive and minimum negative of each group of positive and negative values without any sorting. For the example given the result b would be:
b{1,1}= [-3;4]
b{1,2}= [3;-7;5;-6]
Thanks

댓글 수: 2

How is a "group" to be determined ? Are the group boundaries always marked by 0 in the first column? If so is the value in the second column of that row to be included or excluded from the calculations? Would you not also be wanting to record the boundaries of the groups found?
Dana
Dana 2014년 4월 1일
Each column must be analyzed independently, they are not connected. The boundary between groups is the change of sign. In the example, the first column would have two groups: [0;-3;-2] and [0;4;1]. The aim is find the minimum of the negative values and the maximum of the positive ones. The first group is a negative one and the minimum of them is -3. The second group is positive and the maximum is 4. The answer for first column is therefore [-3;4]. For the second column there are four groups:[1;3] [-7] [5] and [-6;-2]. The answer for the second column would be then [3;-7;5;-6]. And yes, I would like to record the boundaries of the groups also... Thanks

댓글을 달려면 로그인하십시오.

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2014년 4월 1일
편집: Andrei Bobrov 2014년 4월 1일
a=[0 1;-3 3;-2 -7;0 5;4 -6;1 -2]
t = [true(1,size(a,2));diff(a<0)~=0];
ir = cumsum(t);
jj = max(ir);
ii = bsxfun(@plus,ir,[0,jj(1:end-1)]);
out1 = mat2cell(accumarray(ii(:),a(:),[],@(x)max(abs(x))*sign(sum(x))),jj,1);
out = cellfun(@(x)x(x~=0),out1,'un',0);

댓글 수: 1

Dana
Dana 2014년 4월 1일
Great! The only problem comes when there are Nans in the source data but it works for the rest of cases! I've got another solution but it's really inefficient...problems of being new with matlab! Thanks a lot!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Joseph Cheng
Joseph Cheng 2014년 3월 31일
Why not break it up into the groups and handle them in a for loop? you can search for groups with groupstart = find(a(:,1)==0)
groupstart = find(a(:,1)==0)
after that you can use a for loop that does the min and max for a(groupstart(i):groupstart(i+1)-1,:)

댓글 수: 1

Dana
Dana 2014년 4월 1일
Thanks for that but the problem with this comes when there is a change of sign with no zero to be found by the find command.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

2014년 3월 31일

댓글:

2014년 4월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by