Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Matrix matching

Subject: Matrix matching

From: charles atlas

Date: 13 Aug, 2009 17:55:30

Message: 1 of 13

I have two data sets
A =
1 23 345
1 45 636
1 65 672
2 26 985
2 86 350
3 34 452
3 98 768
3 56 665
3 39 445
4 34 678
5 65 123
5 17 435
5 78 198
6 23 234
6 44 760

B =
1 23 345
1 45 636
1 65 672
2 26 985
2 86 350
3 34 452
3 98 768
3 56 665
3 39 445
4 34 678
5 65 123
5 17 435
5 78 198
6 23 234
6 44 760

Obviously these two data sets are the same.
However, sometimes I will have two data sets that are slightly off where A would have one two or three less rows than B or vice versa as shown:
A =
1 23 345
1 45 636
1 65 672
2 26 985
2 86 350
3 34 452
3 98 768
3 56 665
3 39 445
4 34 678
5 65 123
5 17 435
5 78 198
6 23 234
6 44 760

B =
1 23 345
1 45 636
2 26 985
2 86 350
3 34 452
3 98 768
3 56 665
4 34 678
5 65 123
5 17 435
5 78 198
6 23 234
6 44 760

What I would like to do is make it so that they have the same number of rows (possibly by inserting a row of zeros) anytime this problem occurs.
Final result would look like this:
 
A =
1 23 345
1 45 636
1 65 672
2 26 985
2 86 350
3 34 452
3 98 768
3 56 665
3 39 445
4 34 678
5 65 123
5 17 435
5 78 198
6 23 234
6 44 760

B =
1 23 345
1 45 636
0 00 000
2 26 985
2 86 350
3 34 452
3 98 768
3 56 665
0 00 000
4 34 678
5 65 123
5 17 435
5 78 198
6 23 234
6 44 760

If anyone knows how to do this, Help is greatly appreciated
-Charlie

Subject: Matrix matching

From: us

Date: 13 Aug, 2009 18:30:21

Message: 2 of 13

"charles atlas"
> If anyone knows how to do this, Help is greatly appreciated
> -Charlie

to stupidly copy/paste hundreds of lines of idiosyncratic and (therefore) useless data into this NG is considered unintelligent behavior by most CSSMers...
come up with a small input/output sample...

us

Subject: Matrix matching

From: charles atlas

Date: 13 Aug, 2009 19:09:02

Message: 3 of 13

"us " <us@neurol.unizh.ch> wrote in message <h61m3t$kcd$1@fred.mathworks.com>...
> "charles atlas"
> > If anyone knows how to do this, Help is greatly appreciated
> > -Charlie
>
> to stupidly copy/paste hundreds of lines of idiosyncratic and (therefore) useless data into this NG is considered unintelligent behavior by most CSSMers...
> come up with a small input/output sample...
>
> us

Woa buddy, first of all I was not trying to be a jerk or display too much information to the point where people would get confused and/or get annoyed.
I posted that much information to be sure that people knew what I meant.
If you are that offended by the overly large amount of information that I posted then you have either way too much time on your hands to waste a few minutes out of your life with the sole purpose of ridiculing me or you are somebody that gets a rise out of putting other people down while creating the illusion that you are helping them use the forums better. Either way you are not helping but you are merely making other people feel worse about themselves.
I'll tell you what I am going to do.
-I will take your advice and when I post on the forums again I will use smaller examples.
-I will continue to wait for somebody that has any information that will actually be beneficial to my time and will help me solve my dilemma.

Subject: Matrix matching

From: Perttu Ranta-aho

Date: 13 Aug, 2009 19:20:07

Message: 4 of 13

One of the many solutions:
>> a=1:5;b=[1 3 5];c=zeros(size(a));
>> [dummy,a_iind]=ismember(b,a);
>> c(a_iind) = b;

If I got your problem right.

_perttu

Subject: Matrix matching

From: someone

Date: 13 Aug, 2009 19:21:02

Message: 5 of 13

"charles atlas" <mufasa1234555@yahoo.com> wrote in message <h61k2i$p69$1@fred.mathworks.com>...
> I have two data sets
> A =
> 1 23 345
> 1 45 636
> 1 65 672
> 2 26 985
> 2 86 350
> 3 34 452
> 3 98 768
> 3 56 665
> 3 39 445
> 4 34 678
> 5 65 123
> 5 17 435
> 5 78 198
> 6 23 234
> 6 44 760
>
> B =
> 1 23 345
> 1 45 636
> 1 65 672
> 2 26 985
> 2 86 350
> 3 34 452
> 3 98 768
> 3 56 665
> 3 39 445
> 4 34 678
> 5 65 123
> 5 17 435
> 5 78 198
> 6 23 234
> 6 44 760
>
> Obviously these two data sets are the same.
> However, sometimes I will have two data sets that are slightly off where A would have one two or three less rows than B or vice versa as shown:
> A =
> 1 23 345
> 1 45 636
> 1 65 672
> 2 26 985
> 2 86 350
> 3 34 452
> 3 98 768
> 3 56 665
> 3 39 445
> 4 34 678
> 5 65 123
> 5 17 435
> 5 78 198
> 6 23 234
> 6 44 760
>
> B =
> 1 23 345
> 1 45 636
> 2 26 985
> 2 86 350
> 3 34 452
> 3 98 768
> 3 56 665
> 4 34 678
> 5 65 123
> 5 17 435
> 5 78 198
> 6 23 234
> 6 44 760
>
> What I would like to do is make it so that they have the same number of rows (possibly by inserting a row of zeros) anytime this problem occurs.
> Final result would look like this:
>
> A =
> 1 23 345
> 1 45 636
> 1 65 672
> 2 26 985
> 2 86 350
> 3 34 452
> 3 98 768
> 3 56 665
> 3 39 445
> 4 34 678
> 5 65 123
> 5 17 435
> 5 78 198
> 6 23 234
> 6 44 760
>
> B =
> 1 23 345
> 1 45 636
> 0 00 000
> 2 26 985
> 2 86 350
> 3 34 452
> 3 98 768
> 3 56 665
> 0 00 000
> 4 34 678
> 5 65 123
> 5 17 435
> 5 78 198
> 6 23 234
> 6 44 760
>
> If anyone knows how to do this, Help is greatly appreciated
> -Charlie

% A hint:
doc ismember

% Look at the the 'rows' option of that function.

% Then take it from there.

Subject: Matrix matching

From: Matt

Date: 13 Aug, 2009 19:24:19

Message: 6 of 13

indices=ismember(A,B,'rows');
Bnew=zeros(size(A));
Bnew(indices,:)=A(indices,:);

Subject: Matrix matching

From: Matt

Date: 13 Aug, 2009 19:33:02

Message: 7 of 13

"charles atlas" <mufasa1234555@yahoo.com> wrote in message <h61k2i$p69$1@fred.mathworks.com>...

Just in case it matters, the problem isn't entirely well defined.

Suppose A has identical rows and B is a version of A with some of these rows deleted.
Then it's not clear where the new rows (of zeros) should be inserted into B.

You could insert them anywhere provided that there are enough new rows to equal the number of original rows in A.

Subject: Matrix matching

From: Roger Stafford

Date: 13 Aug, 2009 19:45:18

Message: 8 of 13

"charles atlas" <mufasa1234555@yahoo.com> wrote in message <h61oce$qq7$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <h61m3t$kcd$1@fred.mathworks.com>...
> > to stupidly copy/paste hundreds of lines of idiosyncratic and (therefore) useless data into this NG is considered unintelligent behavior by most CSSMers...
> > come up with a small input/output sample...
> > us
>
> Woa buddy, first of all I was not trying to be a jerk or display too much information to the point where people would get confused and/or get annoyed.
> I posted that much information to be sure that people knew what I meant.
> If you are that offended by the overly large amount of information that I posted then you have either way too much time on your hands to waste a few minutes out of your life with the sole purpose of ridiculing me or you are somebody that gets a rise out of putting other people down while creating the illusion that you are helping them use the forums better. Either way you are not helping but you are merely making other people feel worse about themselves.
> I'll tell you what I am going to do.
> -I will take your advice and when I post on the forums again I will use smaller examples.
> -I will continue to wait for somebody that has any information that will actually be beneficial to my time and will help me solve my dilemma.

  I agree with Urs that you have devoted too much of your text giving extended array examples and too little explaining in good English what you mean. As an example of the latter, you have not specified whether you wish to treat an A-B pair in which rows in A and in B can be encountered in which neither row can ever be matched with a row in the other. A good algorithm to carry out your desired action is going to run up against just that possible question. Also are you guaranteeing that all matching between elements represent monotonicity - that is, if row i1 of A and j1 of B match and similarly i2 of A and j2 of B, do you always have i1 < i2 and j1 < j2 (or visa versa?)

  It would be far better to spend time giving a much more precise explanation of your problem than in presenting long but overly obvious examples.

Roger Stafford

Subject: Matrix matching

From: someone

Date: 13 Aug, 2009 19:45:18

Message: 9 of 13

"Matt " <xys@whatever.com> wrote in message <h61ppe$3t3$1@fred.mathworks.com>...
> "charles atlas" <mufasa1234555@yahoo.com> wrote in message <h61k2i$p69$1@fred.mathworks.com>...
>
> Just in case it matters, the problem isn't entirely well defined.
>
> Suppose A has identical rows and B is a version of A with some of these rows deleted.
> Then it's not clear where the new rows (of zeros) should be inserted into B.
>
> You could insert them anywhere provided that there are enough new rows to equal the number of original rows in A.

Also, I believe as the OP stated the original problem,
you also have to ensure that size(A) >= size(B)

I believe he statedsomething like:
 A has more rows than B or vice versa.

Subject: Matrix matching

From: us

Date: 13 Aug, 2009 21:52:01

Message: 10 of 13

"charles atlas"
> Woa buddy
> I'll tell you what I am going to do.
> -I will take your advice and when I post on the forums again I will use smaller examples...

...an excellent choice for your future on CSSM...

us

Subject: Matrix matching

From: charles atlas

Date: 14 Aug, 2009 15:08:19

Message: 11 of 13

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <h61qge$mgt$1@fred.mathworks.com>...
> "charles atlas" <mufasa1234555@yahoo.com> wrote in message <h61oce$qq7$1@fred.mathworks.com>...
> > "us " <us@neurol.unizh.ch> wrote in message <h61m3t$kcd$1@fred.mathworks.com>...
> > > to stupidly copy/paste hundreds of lines of idiosyncratic and (therefore) useless data into this NG is considered unintelligent behavior by most CSSMers...
> > > come up with a small input/output sample...
> > > us
> >
> > Woa buddy, first of all I was not trying to be a jerk or display too much information to the point where people would get confused and/or get annoyed.
> > I posted that much information to be sure that people knew what I meant.
> > If you are that offended by the overly large amount of information that I posted then you have either way too much time on your hands to waste a few minutes out of your life with the sole purpose of ridiculing me or you are somebody that gets a rise out of putting other people down while creating the illusion that you are helping them use the forums better. Either way you are not helping but you are merely making other people feel worse about themselves.
> > I'll tell you what I am going to do.
> > -I will take your advice and when I post on the forums again I will use smaller examples.
> > -I will continue to wait for somebody that has any information that will actually be beneficial to my time and will help me solve my dilemma.
>
> I agree with Urs that you have devoted too much of your text giving extended array examples and too little explaining in good English what you mean. As an example of the latter, you have not specified whether you wish to treat an A-B pair in which rows in A and in B can be encountered in which neither row can ever be matched with a row in the other. A good algorithm to carry out your desired action is going to run up against just that possible question. Also are you guaranteeing that all matching between elements represent monotonicity - that is, if row i1 of A and j1 of B match and similarly i2 of A and j2 of B, do you always have i1 < i2 and j1 < j2 (or visa versa?)
>
> It would be far better to spend time giving a much more precise explanation of your problem than in presenting long but overly obvious examples.
>
> Roger Stafford


I am sorry if I did not fully explain my problem. Let me try to explain what I meant in more detail.

data sets A and B will always be the same. That is to say the rows of each data set will always correspond to each other. Only in certian cases will some rows from A OR B be missing. If A has a few missing rows, B will always have the right amount of rows of data. If B has a few missing rows than A will have the right amount of rows. In other cases both A and B will have the right amount of rows and they will be the exact same data sets. In this case I want the program to recognize that and to not change either of the two sets of data. I only want the program to insert a row of zeros into either of the two data sets that contains the missing rows.

My intentions when I posted this message were not to irritate anyone who runs or who uses these forums. I simply am a very visual person and I wanted to show everybody what I meant using a large set of data. This is because if I were another person using these forums and I saw this post, I would better understand that person's questions by seeing exactly what they needed to do and all of their data as apposed to having thier problem explained to me via a verbal explanation.

Subject: Matrix matching

From: someone

Date: 14 Aug, 2009 15:35:13

Message: 12 of 13

"charles atlas" <mufasa1234555@yahoo.com> wrote in message <h63ul3$hn5$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <h61qge$mgt$1@fred.mathworks.com>...
> > "charles atlas" <mufasa1234555@yahoo.com> wrote in message <h61oce$qq7$1@fred.mathworks.com>...
> > > "us " <us@neurol.unizh.ch> wrote in message <h61m3t$kcd$1@fred.mathworks.com>...
> > > > to stupidly copy/paste hundreds of lines of idiosyncratic and (therefore) useless data into this NG is considered unintelligent behavior by most CSSMers...
> > > > come up with a small input/output sample...
> > > > us
> > >
> > > Woa buddy, first of all I was not trying to be a jerk or display too much information to the point where people would get confused and/or get annoyed.
> > > I posted that much information to be sure that people knew what I meant.
> > > If you are that offended by the overly large amount of information that I posted then you have either way too much time on your hands to waste a few minutes out of your life with the sole purpose of ridiculing me or you are somebody that gets a rise out of putting other people down while creating the illusion that you are helping them use the forums better. Either way you are not helping but you are merely making other people feel worse about themselves.
> > > I'll tell you what I am going to do.
> > > -I will take your advice and when I post on the forums again I will use smaller examples.
> > > -I will continue to wait for somebody that has any information that will actually be beneficial to my time and will help me solve my dilemma.
> >
> > I agree with Urs that you have devoted too much of your text giving extended array examples and too little explaining in good English what you mean. As an example of the latter, you have not specified whether you wish to treat an A-B pair in which rows in A and in B can be encountered in which neither row can ever be matched with a row in the other. A good algorithm to carry out your desired action is going to run up against just that possible question. Also are you guaranteeing that all matching between elements represent monotonicity - that is, if row i1 of A and j1 of B match and similarly i2 of A and j2 of B, do you always have i1 < i2 and j1 < j2 (or visa versa?)
> >
> > It would be far better to spend time giving a much more precise explanation of your problem than in presenting long but overly obvious examples.
> >
> > Roger Stafford
>
>
> I am sorry if I did not fully explain my problem. Let me try to explain what I meant in more detail.
>
> data sets A and B will always be the same. That is to say the rows of each data set will always correspond to each other. Only in certian cases will some rows from A OR B be missing. If A has a few missing rows, B will always have the right amount of rows of data. If B has a few missing rows than A will have the right amount of rows. In other cases both A and B will have the right amount of rows and they will be the exact same data sets. In this case I want the program to recognize that and to not change either of the two sets of data. I only want the program to insert a row of zeros into either of the two data sets that contains the missing rows.
>
> My intentions when I posted this message were not to irritate anyone who runs or who uses these forums. I simply am a very visual person and I wanted to show everybody what I meant using a large set of data. This is because if I were another person using these forums and I saw this post, I would better understand that person's questions by seeing exactly what they needed to do and all of their data as apposed to having thier problem explained to me via a verbal explanation.

OK, so read the above responses and put them together.

The pseudocode should look something like:

if ~isequal(A,B)
   if numel(A) > numel(B)
      ismember(A,B,'rows')
        % Now you know the missing rows in B
        % insert code here to stuff zeros in them
   else
      ismember(B,A,'rows')
         % same as above but swap A & B
   end
end

I don't have MATLAB with me now.
So, I'm sure the above syntax is NOT correct.
But, it should be enough to get you started
(with the advice you received i the other responses).

Subject: Matrix matching

From: Andy

Date: 14 Aug, 2009 15:38:19

Message: 13 of 13

"charles atlas" <mufasa1234555@yahoo.com> wrote in message <h63ul3$hn5$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <h61qge$mgt$1@fred.mathworks.com>...
> > "charles atlas" <mufasa1234555@yahoo.com> wrote in message <h61oce$qq7$1@fred.mathworks.com>...
> > > "us " <us@neurol.unizh.ch> wrote in message <h61m3t$kcd$1@fred.mathworks.com>...
> > > > to stupidly copy/paste hundreds of lines of idiosyncratic and (therefore) useless data into this NG is considered unintelligent behavior by most CSSMers...
> > > > come up with a small input/output sample...
> > > > us
> > >
> > > Woa buddy, first of all I was not trying to be a jerk or display too much information to the point where people would get confused and/or get annoyed.
> > > I posted that much information to be sure that people knew what I meant.
> > > If you are that offended by the overly large amount of information that I posted then you have either way too much time on your hands to waste a few minutes out of your life with the sole purpose of ridiculing me or you are somebody that gets a rise out of putting other people down while creating the illusion that you are helping them use the forums better. Either way you are not helping but you are merely making other people feel worse about themselves.
> > > I'll tell you what I am going to do.
> > > -I will take your advice and when I post on the forums again I will use smaller examples.
> > > -I will continue to wait for somebody that has any information that will actually be beneficial to my time and will help me solve my dilemma.
> >
> > I agree with Urs that you have devoted too much of your text giving extended array examples and too little explaining in good English what you mean. As an example of the latter, you have not specified whether you wish to treat an A-B pair in which rows in A and in B can be encountered in which neither row can ever be matched with a row in the other. A good algorithm to carry out your desired action is going to run up against just that possible question. Also are you guaranteeing that all matching between elements represent monotonicity - that is, if row i1 of A and j1 of B match and similarly i2 of A and j2 of B, do you always have i1 < i2 and j1 < j2 (or visa versa?)
> >
> > It would be far better to spend time giving a much more precise explanation of your problem than in presenting long but overly obvious examples.
> >
> > Roger Stafford
>
>
> I am sorry if I did not fully explain my problem. Let me try to explain what I meant in more detail.
>
> data sets A and B will always be the same. That is to say the rows of each data set will always correspond to each other. Only in certian cases will some rows from A OR B be missing. If A has a few missing rows, B will always have the right amount of rows of data. If B has a few missing rows than A will have the right amount of rows. In other cases both A and B will have the right amount of rows and they will be the exact same data sets. In this case I want the program to recognize that and to not change either of the two sets of data. I only want the program to insert a row of zeros into either of the two data sets that contains the missing rows.
>
> My intentions when I posted this message were not to irritate anyone who runs or who uses these forums. I simply am a very visual person and I wanted to show everybody what I meant using a large set of data. This is because if I were another person using these forums and I saw this post, I would better understand that person's questions by seeing exactly what they needed to do and all of their data as apposed to having thier problem explained to me via a verbal explanation.

% A short example is visually clearer.
% Does this accurately describe what you want?

% input:
A=[1 1;
     1 1;
     1 2;
     2 3];

B=[1 1;
     2 3];

% desired output:
C=[1 1;
     0 0;
     0 0;
     2 3];

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us