http://www.mathworks.com/matlabcentral/newsreader/view_thread/310568
MATLAB Central Newsreader  Complex Matrix Row Deletion
Feed for thread: Complex Matrix Row Deletion
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Sun, 17 Jul 2011 18:00:10 +0000
Complex Matrix Row Deletion
http://www.mathworks.com/matlabcentral/newsreader/view_thread/310568#845732
curoch
Hi all,<br>
<br>
My problem is as follows:<br>
<br>
 I have a large matrix which may be exemplified in the following manner<br>
<br>
A = [1 1 23<br>
1 1 24<br>
1 1 34<br>
1 1 45<br>
2 1 77<br>
2 1 12<br>
2 1 4 ] <br>
<br>
 My aim is to analyse the elements in the third column for each set where the values in column 1 and 2 are equal.<br>
 Upon analysing, I need to delete the row which has the smallest value in column 3.<br>
<br>
For example, applying this to matrix A<br>
<br>
The first set is [ 1 1 23<br>
1 1 24] hence deleting the first row<br>
<br>
Matrix A thus becomes<br>
<br>
A = [ 1 1 23<br>
1 1 34<br>
2 1 12<br>
2 1 4]<br>
<br>
Thanks a lot for any help,<br>
<br>
Regards,<br>
<br>
F

Sun, 17 Jul 2011 19:17:27 +0000
Re: Complex Matrix Row Deletion
http://www.mathworks.com/matlabcentral/newsreader/view_thread/310568#845735
dpb
On 7/17/2011 1:00 PM, curoch wrote:<br>
> Hi all,<br>
><br>
> My problem is as follows:<br>
><br>
>  I have a large matrix which may be exemplified in the following<br>
> manner<br>
><br>
> A = [1 1 23<br>
> 1 1 24<br>
...<br>
> 2 1 4 ]<br>
>  My aim is to analyse the elements in the third column for each set<br>
> where the values in column 1 and 2 are equal.<br>
<br>
i<br>
>  Upon analysing, I need to delete the row which has the smallest value<br>
> in column 3.<br>
><br>
> For example, applying this to matrix A<br>
><br>
> The first set is [ 1 1 23<br>
> 1 1 24] hence deleting the first row<br>
<br>
>> idx=A(:,1)==A(:,2);<br>
>> z=A(idx,3)<br>
<br>
z =<br>
<br>
23<br>
24<br>
<br>
>> A(idx(1),:)=[]<br>
A =<br>
1 1 24<br>
1 1 34<br>
1 1 45<br>
2 1 77<br>
2 1 12<br>
2 1 4<br>
<br>
>><br>
><br>
> Matrix A thus becomes<br>
><br>
> A = [ 1 1 23<br>
> 1 1 34<br>
> 2 1 12<br>
> 2 1 4]<br>
><br>
...<br>
<br>
I don't see the above from the description and the matrix given but <br>
perhaps the previous manipulations will be of some benefit in selecting <br>
and deselecting desired rows, etc., ...<br>
<br>


Mon, 18 Jul 2011 00:11:08 +0000
Re: Complex Matrix Row Deletion
http://www.mathworks.com/matlabcentral/newsreader/view_thread/310568#845745
Roger Stafford
"curoch" wrote in message <ivv7va$kan$1@newscl01ah.mathworks.com>...<br>
> ........<br>
> A = [1 1 23<br>
> 1 1 24<br>
> 1 1 34<br>
> 1 1 45<br>
> 2 1 77<br>
> 2 1 12<br>
> 2 1 4 ] <br>
> <br>
>  My aim is to analyse the elements in the third column for each set where the values in column 1 and 2 are equal.<br>
>  Upon analysing, I need to delete the row which has the smallest value in column 3.<br>
> <br>
> For example, applying this to matrix A<br>
> <br>
> The first set is [ 1 1 23<br>
> 1 1 24] hence deleting the first row<br>
> <br>
> Matrix A thus becomes<br>
> <br>
> A = [ 1 1 23<br>
> 1 1 34<br>
> 2 1 12<br>
> 2 1 4]<br>
> ......<br>
         <br>
Perhaps you should clarify your question, Curoch. You stated "I need to delete the row which has the smallest value in column 3" but then you proceeded to delete the larger one instead. Also you haven't said what is to happen if three or more rows have column 1 all alike and column 2 all alike. How many are to be deleted and which one(s)? In the case of the single row with 2 and 1 you didn't delete anything at all, so what is the proper generalization?<br>
<br>
Roger Stafford

Mon, 18 Jul 2011 15:52:09 +0000
Re: Complex Matrix Row Deletion
http://www.mathworks.com/matlabcentral/newsreader/view_thread/310568#845805
curoch
"Roger Stafford" wrote in message <ivvtms$dil$1@newscl01ah.mathworks.com>...<br>
> "curoch" wrote in message <ivv7va$kan$1@newscl01ah.mathworks.com>...<br>
> > ........<br>
> > A = [1 1 23<br>
> > 1 1 24<br>
> > 1 1 34<br>
> > 1 1 45<br>
> > 2 1 77<br>
> > 2 1 12<br>
> > 2 1 4 ] <br>
> > <br>
> >  My aim is to analyse the elements in the third column for each set where the values in column 1 and 2 are equal.<br>
> >  Upon analysing, I need to delete the row which has the smallest value in column 3.<br>
> > <br>
> > For example, applying this to matrix A<br>
> > <br>
> > The first set is [ 1 1 23<br>
> > 1 1 24] hence deleting the first row<br>
> > <br>
> > Matrix A thus becomes<br>
> > <br>
> > A = [ 1 1 23<br>
> > 1 1 34<br>
> > 2 1 12<br>
> > 2 1 4]<br>
> > ......<br>
>          <br>
> Perhaps you should clarify your question, Curoch. You stated "I need to delete the row which has the smallest value in column 3" but then you proceeded to delete the larger one instead. Also you haven't said what is to happen if three or more rows have column 1 all alike and column 2 all alike. How many are to be deleted and which one(s)? In the case of the single row with 2 and 1 you didn't delete anything at all, so what is the proper generalization?<br>
> <br>
> Roger Stafford<br>
<br>
Dear Roger,<br>
<br>
Thank you for replying. It appears as you have stated that my post contains some inaccuracies which obviously makes it harder for you guys to help me. I hope this post will make everything clear.<br>
<br>
1. I should have said "Delete the row with the largest element in Column 3" thus deleting the 2nd row with element 24 in column 3. My apologies. Unfortunately, I cannot edit my previous post so please ignore it.<br>
<br>
2. In terms of proper generalisation, the best way I can explain is by partitioning the matrix. A new example<br>
<br>
B = [ 1 1 123<br>
1 1 434<br>
1 1 111<br>
1 1 434<br>
1 1 666<br>
2 1 12<br>
2 1 44<br>
2 1 33 ]<br>
<br>
I need to analyse each set within matrix B where the element in column 1 is equal. So the first set is as follows<br>
<br>
[ 1 1 123, 1 1 434, 1 1 111, 1 1 434, 1 1 666]<br>
<br>
Within each set, I need to first of all analyse the rows where the elements in column 1 and 2 are equal. Therefore [1 1 123, 1 1 434, 1 1 111].<br>
<br>
From there, I need to keep the row with the smallest element in column 3.<br>
<br>
I then need to analyse the remaining rows within set 1 which is [1 1 434, 1 1 666], also keeping the row with the smallest element in column 3.<br>
<br>
These "sets" or "partitions" vary in length and the same may be said about the inner sets. Some sets have a length of 5, others 2 etc...<br>
<br>
3. In the case of a single row (where column 1 = column 2 and column 1 ~= column 2), nothing is performed on the row.<br>
<br>
4. To add, for any set of rows, the element in column 3 will never be equal so there will never be an instance when there is no minimum.<br>
<br>
Upon successful completion, matrix B will take the following appearance<br>
<br>
B = [ 1 1 111<br>
1 1 434<br>
2 1 12<br>
2 1 33]<br>
<br>
5. Just to clarify, the elements in column 1 and 2 are more complex than 1 and 1, the matrix could be in this form for instance<br>
<br>
C = [ 2.321 2.321 324<br>
2.321 2.321 121<br>
2.321 0.432 111<br>
2.321 0.432 114 ]<br>
<br>
6. As a last point, in each set, there are only one possibility when column 1 does not equal column 2.<br>
<br>
For instance, the matrix can never take the following form<br>
<br>
D = [ 12 0.5 1<br>
12 0.5 2<br>
12 1 3<br>
12 10 4]<br>
<br>
I hope this clears everything up,<br>
<br>
Do not hesitate to let me know if something needs clarifying,<br>
<br>
Regards,<br>
<br>
CF

Mon, 18 Jul 2011 16:37:08 +0000
Re: Complex Matrix Row Deletion
http://www.mathworks.com/matlabcentral/newsreader/view_thread/310568#845808
Roger Stafford
"curoch" wrote in message <j01kr9$93u$1@newscl01ah.mathworks.com>...<br>
> ......<br>
> B = [ 1 1 123<br>
> 1 1 434<br>
> 1 1 111<br>
> 1 1 434<br>
> 1 1 666<br>
> 2 1 12<br>
> 2 1 44<br>
> 2 1 33 ]<br>
> ......<br>
> Upon successful completion, matrix B will take the following appearance<br>
> <br>
> B = [ 1 1 111<br>
> 1 1 434<br>
> 2 1 12<br>
> 2 1 33]<br>
> .........<br>
> 6. As a last point, in each set, there are only one possibility when column 1 does not equal column 2.<br>
> <br>
> For instance, the matrix can never take the following form<br>
> <br>
> D = [ 12 0.5 1<br>
> 12 0.5 2<br>
> 12 1 3<br>
> 12 10 4]<br>
> ........<br>
         <br>
A = sortrows(A);<br>
[~,m] = unique(A(:,1:2),'rows','first');<br>
A = A(m,:);<br>
<br>
Notes:<br>
<br>
1. The first line with 'sortrows' will alter the order of the rows if they are not already in lexicographic order.<br>
<br>
2. If your elements are of fractional form, such as the 2.321 of your example, the comparison of elements done by 'unique' nevertheless requires exact equality. Two numbers can display as 2.321 and yet not be exactly equal, perhaps differing only in the least few bits.<br>
<br>
3. I could not make sense of your point 6. I don't understand what there is about the form of example D that your matrix can never take.<br>
<br>
Roger Stafford

Mon, 18 Jul 2011 17:50:56 +0000
Re: Complex Matrix Row Deletion
http://www.mathworks.com/matlabcentral/newsreader/view_thread/310568#845816
dpb
On 7/18/2011 11:37 AM, Roger Stafford wrote:<br>
...<br>
<br>
> 3. I could not make sense of your point 6. I don't understand what there<br>
> is about the form of example D that your matrix can never take.<br>
...<br>
<br>
I think he's saying that the generating process for the original array <br>
is such that if A(:,1)~=A(:,2) for some (set of) (:) that there is only <br>
one occurrence of that particular combinationiow, (:)>n where n is a <br>
single value, not a vector. This would only seem to be significant in <br>
that in that case there isn't a row to be thrown away...<br>
<br>


Tue, 19 Jul 2011 06:22:07 +0000
Re: Complex Matrix Row Deletion
http://www.mathworks.com/matlabcentral/newsreader/view_thread/310568#845877
Bruno Luong
Try this:<br>
<br>
B = [ 1 1 123<br>
1 1 434<br>
1 1 111<br>
1 1 434<br>
1 1 666<br>
2 1 12<br>
2 1 44<br>
2 1 33 ]<br>
<br>
[S I J] = unique(B(:,1:2),'rows');<br>
B = [S accumarray(J,B(:,3),[],@min)]<br>
<br>
% Bruno

Tue, 19 Jul 2011 07:08:08 +0000
Re: Complex Matrix Row Deletion
http://www.mathworks.com/matlabcentral/newsreader/view_thread/310568#845883
Bruno Luong
"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <j037qf$pna$1@newscl01ah.mathworks.com>...<br>
<br>
> [S I J] = unique(B(:,1:2),'rows');<br>
> B = [S accumarray(J,B(:,3),[],@min)]<br>
> <br>
<br>
If you prefer to order B same as the original, add these two commands :<br>
<br>
[trash idx]=sort(I);<br>
B = B(idx,:);<br>
<br>
% Bruno

Tue, 19 Jul 2011 21:48:09 +0000
Re: Complex Matrix Row Deletion
http://www.mathworks.com/matlabcentral/newsreader/view_thread/310568#845969
curoch
"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <j03agn$2lt$1@newscl01ah.mathworks.com>...<br>
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <j037qf$pna$1@newscl01ah.mathworks.com>...<br>
> <br>
> > [S I J] = unique(B(:,1:2),'rows');<br>
> > B = [S accumarray(J,B(:,3),[],@min)]<br>
> > <br>
> <br>
> If you prefer to order B same as the original, add these two commands :<br>
> <br>
> [trash idx]=sort(I);<br>
> B = B(idx,:);<br>
> <br>
> % Bruno<br>
<br>
Dear Bruno,<br>
<br>
Thank you so much. To extend your answer<br>
<br>
If matrix B were of size (m,6) where column 3,5,6 in the new matrix would correspond to column 1,2,3 in the old example, how would I proceed so that I could keep all columns in the new matrix rather than deleting columns 1,2 and 4?<br>
<br>
@ Roger Stafford<br>
<br>
Thank you very much for your comments and assistance<br>
<br>
@ dpb<br>
<br>
Your comment is exactly what I was trying to get at.

Wed, 20 Jul 2011 07:20:09 +0000
Re: Complex Matrix Row Deletion
http://www.mathworks.com/matlabcentral/newsreader/view_thread/310568#845993
Bruno Luong
"curoch" wrote in message <j04u2p$1f4$1@newscl01ah.mathworks.com>...<br>
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <br>
> <br>
> Thank you so much. To extend your answer<br>
> <br>
> If matrix B were of size (m,6) where column 3,5,6 in the new matrix would correspond to column 1,2,3 in the old example, how would I proceed so that I could keep all columns in the new matrix rather than deleting columns 1,2 and 4?<br>
> <br>
<br>
function imin = findminloc(loc, B3)<br>
[trash, imin] = min(B3(loc));<br>
imin = loc(imin);<br>
end % findminloc, save it in another file<br>
<br>
%% test script<br>
% Data<br>
B = rand(8,6);<br>
B(:,4:6) = [ 1 1 123<br>
1 1 434<br>
1 1 111<br>
1 1 434<br>
1 1 666<br>
2 1 12<br>
2 1 44<br>
2 1 33 ]<br>
<br>
% Engine<br>
[S I J] = unique(B(:,4:5),'rows');<br>
m = size(B,1);<br>
I = accumarray(J,(1:m)',[], @(loc) findminloc(loc, B(:,6)));<br>
B = B(sort(I),:)<br>
<br>
% Bruno