there is a matrix like
A=[ 0 1 0;
0 2 0;
1 0 0;
1 0 1];
compering A(1,:) and A(2,:), i choose [0 2 0];compering A(3,:) and A(4,:), i choose [1 0 1].
the final matrix i want is
A=[ 0 2 0;
1 0 1];
This is just a simple example, if the matrix is M*N, how to use matlab code to get the matrix i want.

댓글 수: 6

what is the comparison criteria
valley
valley 2014년 4월 2일
편집: Azzi Abdelmalek 2014년 4월 2일
the criteria is
a = A(i,:) >= A(j,:);
if all(a) == 1 then A[j,:] = [];
What i and j represent? how are we supposed to know?
valley
valley 2014년 4월 2일
i and j belong to [1:size(a,1)]
Ok, have you tested my edited answer?
valley
valley 2014년 4월 2일
편집: valley 2014년 4월 2일
i did, to this given example, it is ok, but to more complecated cases, such as a big matrix(600*10), it is not successful. m i correct, ur code didnot compare all the rest rows, just check the first row which matches the condition, to the other possible rows urs didnot consider?
sorry to trouble u again, still this problem, with 2 loops will cost much runtime, is there any other efficient way to solve my problem? if u have any idea about that pl tell me, thank u very much.

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

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 4월 2일
편집: Azzi Abdelmalek 2014년 4월 3일

1 개 추천

Edit
A=[1 1 0;0 2 0;1 0 0;1 0 1]
n=size(A,1);
k=1;
while k<n
if any(all(bsxfun(@le,A(k,:),A(k+1:end,:)),2))
A(k,:)=[];
k=k-1;
end
k=k+1;
n=size(A,1);
end
A

댓글 수: 7

valley
valley 2014년 4월 2일
편집: Azzi Abdelmalek 2014년 4월 2일
thank u, but i m afraid your code is not what i want. i think yours is aimed to this specific example. if i change the matrix
A=[1 1 0;0 2 0;1 0 0;1 0 1];
then the final matrix i want is
[1 1 0; 0 2 0;1 0 1]
Azzi Abdelmalek
Azzi Abdelmalek 2014년 4월 2일
편집: Azzi Abdelmalek 2014년 4월 2일
This is not what you asked, look at your question, the result is a 2x3 array. Be more clear, what are your criterion?
Maybe you want this:
A=[1 1 0;0 2 0;1 0 0;1 0 1]
n=size(A,1);
k=1;
while k<n
if all(A(k,:)<=A(k+1,:))
A(k,:)=[];
k=k-1;
end
k=k+1;
n=size(A,1);
end
A
valley
valley 2014년 4월 2일
편집: Azzi Abdelmalek 2014년 4월 2일
sorry 4 the late reply, thx a lot 4 ur kindly help. Though ur code is still not what i want, based on urs i figure out my problem. i want to compare each row with all of the rest rows, but i guess ur codes cannot do that. Based on ur codes, i finish my code as followed
A=[1 1 0;0 2 0;1 0 0;1 0 1]
n=size(A,1);
k=1;
while (k < n)
i = k+1;
while(i < n)
if all(A(k,:) >= A(i,:))
A(i,:) = [];
i = i-1;
n = n-1;
elseif all(A(i,:) >= A(k,:))
A(k,:) = [];
k = k-1;
n = n-1;
break;
else
i = i+1;
n = n ;
end
end
k= k+1;
end
Thank u again.
valley
valley 2014년 4월 2일
sorry to trouble u again, still this problem, with 2 loops will cost much runtime, is there any other efficient way to solve my problem? if u have any idea about that pl tell me, thank u very much.
Try this
n=size(A,1);
k=1;
while k<n
if any(all(bsxfun(@le,A(k,:),A(k+1:end,:)),2))
A(k,:)=[];
k=k-1;
end
k=k+1;
n=size(A,1);
end
A
valley
valley 2014년 4월 3일
thank u so much, it is ok now.

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

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2014년 4월 2일
편집: Andrei Bobrov 2014년 4월 3일

0 개 추천

blockproc(A,[2,3],@(x)max(x.data))
ADD after Valley's comment
cell2mat(accumarray...
(cumsum([true;diff(A(:,1))~=0]),(1:size(A,1))',[],@(x){max(A(x,:),[],1)}))
other variant
out = A(~any(triu(squeeze(all(bsxfun(@ge,A,reshape(A',1,size(A,2),[])),2)),1)),:)

댓글 수: 1

valley
valley 2014년 4월 2일
thx and sorry 4 the late reply, i've fixed my problem wit 2 loops, but urs seems more efficient, i will try later. thank u

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2014년 4월 2일

편집:

2014년 4월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by