COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### How do you do this fast?

• Email
• Follow

```Hi all,

I have sets A, B, C, where C is the biggest set, A is the smallest,

C>B>A, i.e. C contains B contains A.

These are indices indeed.

I have a matrix m x n matrix MatrixC, where C=[1:n].

I have created a matrix MatrixB, which is

MatrixB=MatrixC(:, B).

Now I want to obtain MatrixA, which is

MatrixA=MatrixC(:, A).

---------------------------

However, in reality, actually for cost reasons, I didn't measure all
MatrixC,

instead, I took measurements for MatrixB.

So all I have now is MatrixB, and sets A, B, C.

i.e. the indices A and B are "in C's language".

i.e. the indices A and B are numbered according to their positions in
C=[1:n].

So the question is:

how do I obtain MatrixA from MatrixB and sets A, B, C?

I currently use a "for" loop plus "find" etc. to do this job,

are there any fast approach without using "for" and "find"?

Mine is too slow... too stupid...

Thank you!
```
 0
Reply lunamoonmoon (258) 6/11/2010 8:26:13 PM

See related articles to this posting

```Luna Moon <lunamoonmoon@gmail.com> wrote in message <33a67c34-769a-44db-95c4-1ff9f71dccd9@a42g2000vbl.googlegroups.com>...
> Hi all,
>
> I have sets A, B, C, where C is the biggest set, A is the smallest,
>
> C>B>A, i.e. C contains B contains A.
>
> These are indices indeed.
>
> I have a matrix m x n matrix MatrixC, where C=[1:n].
>
> I have created a matrix MatrixB, which is
>
> MatrixB=MatrixC(:, B).
>
> Now I want to obtain MatrixA, which is
>
> MatrixA=MatrixC(:, A).
>
> ---------------------------
>
> However, in reality, actually for cost reasons, I didn't measure all
> MatrixC,
>
> instead, I took measurements for MatrixB.
>
> So all I have now is MatrixB, and sets A, B, C.
>
> i.e. the indices A and B are "in C's language".
>
> i.e. the indices A and B are numbered according to their positions in
> C=[1:n].
>
> So the question is:
>
> how do I obtain MatrixA from MatrixB and sets A, B, C?
>
> I currently use a "for" loop plus "find" etc. to do this job,
>
> are there any fast approach without using "for" and "find"?
>
> Mine is too slow... too stupid...
>
> Thank you!

Luna,

I dont quite understand what you are trying to do, but what I gather is that you are trying to find the portion A in B and B in C right?  Do you already know the contents of A, B, and C and you just need to find where they are relative to each other?  If so, there may be a better way but I would do this as a template matching problem.  Use normxcorr2 with A as the template for B, and B as the template for C.  Or at least read the help file and examples on normxcorr2 and template matching and see if this is along the lines of what you are attempting to do, otherwise you may want to try and explain better and post the code you have written so far because its tough to follow what your goal is.

Nick
```
 0

```Hmm, your post is a bit confusing.  Could you give a small example?

Luna Moon <lunamoonmoon@gmail.com> wrote in message <33a67c34-769a-

> Hi all,
>
> I have sets A, B, C, where C is the biggest set, A is the smallest,
>
> C>B>A, i.e. C contains B contains A.
>
> These are indices indeed.
>
> I have a matrix m x n matrix MatrixC, where C=[1:n].
>
> I have created a matrix MatrixB, which is
>
> MatrixB=MatrixC(:, B).
>
> Now I want to obtain MatrixA, which is
>
> MatrixA=MatrixC(:, A).
>
> ---------------------------
>
> However, in reality, actually for cost reasons, I didn't measure all
> MatrixC,
>
> instead, I took measurements for MatrixB.
>
> So all I have now is MatrixB, and sets A, B, C.
>
> i.e. the indices A and B are "in C's language".
>
> i.e. the indices A and B are numbered according to their positions in
> C=[1:n].
>
> So the question is:
>
> how do I obtain MatrixA from MatrixB and sets A, B, C?
>
> I currently use a "for" loop plus "find" etc. to do this job,
>
> are there any fast approach without using "for" and "find"?
>
> Mine is too slow... too stupid...
>
> Thank you!
```
 0

```On Jun 11, 6:19=A0pm, "EE Student " <n...@cec.wustl.edu> wrote:
> Luna Moon <lunamoonm...@gmail.com> wrote in message <33a67c34-769a-44db-9=
> > Hi all,
>
> > I have sets A, B, C, where C is the biggest set, A is the smallest,
>
> > C>B>A, i.e. C contains B contains A.
>
> > These are indices indeed.
>
> > I have a matrix m x n matrix MatrixC, where C=3D[1:n].
>
> > I have created a matrix MatrixB, which is
>
> > MatrixB=3DMatrixC(:, B).
>
> > Now I want to obtain MatrixA, which is
>
> > MatrixA=3DMatrixC(:, A).
>
> > ---------------------------
>
> > However, in reality, actually for cost reasons, I didn't measure all
> > MatrixC,
>
> > instead, I took measurements for MatrixB.
>
> > So all I have now is MatrixB, and sets A, B, C.
>
> > i.e. the indices A and B are "in C's language".
>
> > i.e. the indices A and B are numbered according to their positions in
> > C=3D[1:n].
>
> > So the question is:
>
> > how do I obtain MatrixA from MatrixB and sets A, B, C?
>
> > I currently use a "for" loop plus "find" etc. to do this job,
>
> > are there any fast approach without using "for" and "find"?
>
> > Mine is too slow... too stupid...
>
> > Thank you!
>
> Luna,
>
> I dont quite understand what you are trying to do, but what I gather is t=
hat you are trying to find the portion A in B and B in C right? =A0Do you a=
lready know the contents of A, B, and C and you just need to find where the=
y are relative to each other? =A0If so, there may be a better way but I wou=
ld do this as a template matching problem. =A0Use normxcorr2 with A as the =
template for B, and B as the template for C. =A0Or at least read the help f=
ile and examples on normxcorr2 and template matching and see if this is alo=
ng the lines of what you are attempting to do, otherwise you may want to tr=
y and explain better and post the code you have written so far because its =
>
> Nick

Thanks Nick. Sorry for not being clear.

Here is an example:

Originally the data matrix MatrixC is 1000 x 36, so C=3D[1:36].

B=3D[1, 3, 5, 7, 9:36]; so there are 32 numbers in B, they are all
referenced w.r.t. C.

So I have the data matrix MatrixB, consisting of the [1, 3, 5, 7,
9:36] columns from the original data matrix C.

However, due to costs of measurements, I don't have MatrixC, I have
only measured MatrixB.

I also have A=3D[1, 5, 9, 11, 13, 25, 31, 33, 34, 35, 36], all the
numbers of which are again referenced w.r.t C.

And A is B's subset.

I would like to obtain

MatrixA=3DMatrixC(:, A) ideally speaking.

However I don't have MtrixC, I only have MatrixB.

So how do I obtain MatrixA from MatrixB and the index sets A, B, C
without "for" loops?

Thank you
```
 0

```On Jun 11, 6:20=A0pm, "Patrick " <praph...@gmail.com> wrote:
> Hmm, your post is a bit confusing. =A0Could you give a small example?
>
> Luna Moon <lunamoonm...@gmail.com> wrote in message <33a67c34-769a-
>
>
> > Hi all,
>
> > I have sets A, B, C, where C is the biggest set, A is the smallest,
>
> > C>B>A, i.e. C contains B contains A.
>
> > These are indices indeed.
>
> > I have a matrix m x n matrix MatrixC, where C=3D[1:n].
>
> > I have created a matrix MatrixB, which is
>
> > MatrixB=3DMatrixC(:, B).
>
> > Now I want to obtain MatrixA, which is
>
> > MatrixA=3DMatrixC(:, A).
>
> > ---------------------------
>
> > However, in reality, actually for cost reasons, I didn't measure all
> > MatrixC,
>
> > instead, I took measurements for MatrixB.
>
> > So all I have now is MatrixB, and sets A, B, C.
>
> > i.e. the indices A and B are "in C's language".
>
> > i.e. the indices A and B are numbered according to their positions in
> > C=3D[1:n].
>
> > So the question is:
>
> > how do I obtain MatrixA from MatrixB and sets A, B, C?
>
> > I currently use a "for" loop plus "find" etc. to do this job,
>
> > are there any fast approach without using "for" and "find"?
>
> > Mine is too slow... too stupid...
>
> > Thank you!

Thanks Patrick. Sorry for not being clear.

Here is an example:

Originally the data matrix MatrixC is 1000 x 36, so C=3D[1:36].

B=3D[1, 3, 5, 7, 9:36]; so there are 32 numbers in B, they are all
referenced w.r.t. C.

So I have the data matrix MatrixB, consisting of the [1, 3, 5, 7,
9:36] columns from the original data matrix C.

However, due to costs of measurements, I don't have MatrixC, I have
only measured MatrixB.

I also have A=3D[1, 5, 9, 11, 13, 25, 31, 33, 34, 35, 36], all the
numbers of which are again referenced w.r.t C.

And A is B's subset.

I would like to obtain

MatrixA=3DMatrixC(:, A) ideally speaking.

However I don't have MtrixC, I only have MatrixB.

So how do I obtain MatrixA from MatrixB and the index sets A, B, C
without "for" loops?

Thank you
```
 0

```Still clear as mud.  So MatrixC is a 1000 x 36 2-dimensional array (of
36,000 elements total), and C=[1:36] is a one dimensional array that
looks like [1,2,3,4,5,......34,35,36].  Then we have "MtrixC" which I
guess is that same as MatrixC (just a typo).  Plus we have MatrixA,
MatrixB, A, and finally B.

But MatrixB is certain columns from C, so MatrixB must also be a 2D
array.  But C is only a 1-D array (row vector) so it's columns are
only element high, so I guess that means MatrixB is actually also a 1D
row vector.

I gave up at this point.
```
 0

```Luna Moon <lunamoonmoon@gmail.com> wrote in message <8bd25c7a-3b59-4b8f-b6a1-8eb1ebbec132@a1g2000vbl.googlegroups.com>...
> ........
> Here is an example:
> Originally the data matrix MatrixC is 1000 x 36, so C=[1:36].
> B=[1, 3, 5, 7, 9:36]; so there are 32 numbers in B, they are all
> referenced w.r.t. C.
> So I have the data matrix MatrixB, consisting of the [1, 3, 5, 7,
> 9:36] columns from the original data matrix C.
> However, due to costs of measurements, I don't have MatrixC, I have
> only measured MatrixB.
> I also have A=[1, 5, 9, 11, 13, 25, 31, 33, 34, 35, 36], all the
> numbers of which are again referenced w.r.t C.
> And A is B's subset.
> I would like to obtain
> MatrixA=MatrixC(:, A) ideally speaking.
> However I don't have MtrixC, I only have MatrixB.
> So how do I obtain MatrixA from MatrixB and the index sets A, B, C
> without "for" loops?
> .......
- - - - - - -
Does this do what you want?  I too had trouble understanding your question.

[ignore,p] = ismember(A,B);
matrixA = matrixB(:,B(p));

Roger Stafford
```
 0

```"Luna Moon" <lunamoonmoon@gmail.com> wrote in message
On Jun 11, 6:20 pm, "Patrick " <praph...@gmail.com> wrote:
> Hmm, your post is a bit confusing. Could you give a small example?

The example you posted is still too large and general to understand, I
think.  Let's try this.  Run the next four lines of code to generate four
variables (saving any existing variables with those names first, if you
don't want them to be overwritten)

C = 1:4;
MatrixC = reshape(1:20, 5 4);
B = [2 5 3];
MatrixB = MatrixC(:, B);

Now show us how to generate a SMALL sample A and a small sample MatrixA and
use those six variables to explain EXACTLY what you want to receive in the
end and how you want to compute it.  If one of the sample variables I've
given does not satisfy the requirements of your problem, explain why not and
show us how to explicitly create a sample variable that does.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
http://www.mathworks.com

```
 0
Reply slord (13686) 6/14/2010 3:36:12 PM

```"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <huurqj\$jtj\$1@fred.mathworks.com>...
>   Does this do what you want?  I too had trouble understanding your question.
>
>  [ignore,p] = ismember(A,B);
>  matrixA = matrixB(:,B(p));
>
> Roger Stafford
- - - - - -
I made a mistake on that code I sent.  It should have read:

[ignore,p] = ismember(A,B);
matrixA = matrixB(:,p);

Roger Stafford
```
 0