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:
repost problem of find function due to the misaligned text

Subject: repost problem of find function due to the misaligned text

From: Yihui

Date: 27 May, 2011 17:59:05

Message: 1 of 9

Dear Matlab experts,

I have a problem with “find” function.
Assuming that I have a 4 x 3 data as follows:
1 4 7
2 5 8
3 6 9
44 74 12

I would like to selectively output certain number by entering the numbers in the first three rows. For instance, I enter
1 4
2 3
3 6

and then i can get
1 4
2 5
3 6
44 74
as output.

Could you please tell me how to do this? Thanks for any suggestions in advance.
Sincerely,
Yihui

                                                                    

Subject: repost problem of find function due to the misaligned text

From: someone

Date: 27 May, 2011 18:37:04

Message: 2 of 9

"Yihui" wrote in message <iroop9$bvf$1@newscl01ah.mathworks.com>...
> Dear Matlab experts,
>
> I have a problem with “find” function.
> Assuming that I have a 4 x 3 data as follows:
> 1 4 7
> 2 5 8
> 3 6 9
> 44 74 12
>
> I would like to selectively output certain number by entering the numbers in the first three rows. For instance, I enter
> 1 4
> 2 3
> 3 6
>
> and then i can get
> 1 4
> 2 5
> 3 6
> 44 74
> as output.
>
> Could you please tell me how to do this? Thanks for any suggestions in advance.
> Sincerely,
> Yihui
>
>

% Its hard to determine exactly what you are trying do do.
% From your example, why not simply:

>> A = [1 4 7;
2 5 8;
3 6 9;
44 74 12]

A =

     1 4 7
     2 5 8
     3 6 9
    44 74 12

>> B = A(:,1:2)

B =

     1 4
     2 5
     3 6
    44 74

Subject: repost problem of find function due to the misaligned text

From: Yihui

Date: 28 May, 2011 03:36:05

Message: 3 of 9

Hello,

Thanks for the help!

However, in my case, I can not know which column I want. I can only know the index number [1;2;3] or [4;5;6].
The first three numbers in one column are actually coordinates. Therefore, I hope to find the data (e.g., 44) when I enter the coordinates (e.g., [1;2;3]). Or, I hope to find the data (e.g., 44 and 74) when I enter the coordinates (e.g., [1;2;3] and [4;5;6]. Sorry that I did not make myself clearer.

Thanks!
Yihui

"someone" wrote in message <iror0g$ie3$1@newscl01ah.mathworks.com>...
> "Yihui" wrote in message <iroop9$bvf$1@newscl01ah.mathworks.com>...
> > Dear Matlab experts,
> >
> > I have a problem with “find” function.
> > Assuming that I have a 4 x 3 data as follows:
> > 1 4 7
> > 2 5 8
> > 3 6 9
> > 44 74 12
> >
> > I would like to selectively output certain number by entering the numbers in the first three rows. For instance, I enter
> > 1 4
> > 2 3
> > 3 6
> >
> > and then i can get
> > 1 4
> > 2 5
> > 3 6
> > 44 74
> > as output.
> >
> > Could you please tell me how to do this? Thanks for any suggestions in advance.
> > Sincerely,
> > Yihui
> >
> >
>
> % Its hard to determine exactly what you are trying do do.
> % From your example, why not simply:
>
> >> A = [1 4 7;
> 2 5 8;
> 3 6 9;
> 44 74 12]
>
> A =
>
> 1 4 7
> 2 5 8
> 3 6 9
> 44 74 12
>
> >> B = A(:,1:2)
>
> B =
>
> 1 4
> 2 5
> 3 6
> 44 74

Subject: repost problem of find function due to the misaligned text

From: ImageAnalyst

Date: 28 May, 2011 03:44:58

Message: 4 of 9

On May 27, 11:36 pm, "Yihui " <a489930...@yahoo.com.tw> wrote:
> Hello,
>
> Thanks for the help!
>
> However, in my case, I can not know which column I want. I can only know the index number [1;2;3] or [4;5;6].
> The first three numbers in one column are actually coordinates. Therefore, I hope to find the data (e.g., 44) when I enter the coordinates (e.g., [1;2;3]).  Or, I hope to find the data (e.g., 44 and 74) when I enter the coordinates (e.g., [1;2;3] and [4;5;6]. Sorry that I did not make myself clearer.
-----------------------------------------------
You still didn't. I still don't know how the "coordinates" of 1,2,3
mean that you'll take the 4th row, first column. And I don't know how
"coordinates" of [1;2;3] and [4;5;6], which are actually two column
vectors, mean that you'll take all of row 4.

Let's take the first case: 1,2,3. Explain how that means that you
want to extract data(4,1), because I have no idea.

Subject: repost problem of find function due to the misaligned text

From: Yihui

Date: 28 May, 2011 04:35:04

Message: 5 of 9

Hello,

Assuming that I only have 4 x 1 data like [1;2;3;4], I hope to know one function in which I enter [1;2;3] (NOT which row they are in) and then the function returns 4.

Now let's increase the dimension of the data (e.g., I have [1 5 9;2 6 10;3 7 11;4 8 12;]), I enter any two rows of first three numbers (e.g., [1 9;2 10;3 11]) to make the function return [4 12].

Hope this time is better. Thank you!

Yihui


ImageAnalyst <imageanalyst@mailinator.com> wrote in message <0f2a6829-a5b2-4ef0-82c2-e3fa8e1c3d88@d1g2000yqm.googlegroups.com>...
> On May 27, 11:36 pm, "Yihui " <a489930...@yahoo.com.tw> wrote:
> > Hello,
> >
> > Thanks for the help!
> >
> > However, in my case, I can not know which column I want. I can only know the index number [1;2;3] or [4;5;6].
> > The first three numbers in one column are actually coordinates. Therefore, I hope to find the data (e.g., 44) when I enter the coordinates (e.g., [1;2;3]).  Or, I hope to find the data (e.g., 44 and 74) when I enter the coordinates (e.g., [1;2;3] and [4;5;6]. Sorry that I did not make myself clearer.
> -----------------------------------------------
> You still didn't. I still don't know how the "coordinates" of 1,2,3
> mean that you'll take the 4th row, first column. And I don't know how
> "coordinates" of [1;2;3] and [4;5;6], which are actually two column
> vectors, mean that you'll take all of row 4.
>
> Let's take the first case: 1,2,3. Explain how that means that you
> want to extract data(4,1), because I have no idea.

Subject: repost problem of find function due to the misaligned text

From: ImageAnalyst

Date: 28 May, 2011 12:47:18

Message: 6 of 9

Then this will do exactly what you said:

prompt = {'Enter matrix :'};
dlg_title = 'Input your numbers';
num_lines = 1;
def = {'1 2 3',};
answer = inputdlg(prompt,dlg_title,num_lines,def)
values = str2num(answer{1})'
% Check to see if they entered {1,2,3)
if length(values) == 3 && all(values == [1;2;3])
results = 4
else
results = []
end

It's straightforward to adapt it to your second case.

if you want to give rules for how to generalize it for arbitrary
matrices, then go ahead, after all, that is what we've been asking for
and what you've not been giving.
ImageAnalyst

Subject: repost problem of find function due to the misaligned text

From: Roger Stafford

Date: 28 May, 2011 23:53:02

Message: 7 of 9

"Yihui" wrote in message <irpu1o$fhj$1@newscl01ah.mathworks.com>...
> Hello,
>
> Assuming that I only have 4 x 1 data like [1;2;3;4], I hope to know one function in which I enter [1;2;3] (NOT which row they are in) and then the function returns 4.
>
> Now let's increase the dimension of the data (e.g., I have [1 5 9;2 6 10;3 7 11;4 8 12;]), I enter any two rows of first three numbers (e.g., [1 9;2 10;3 11]) to make the function return [4 12].
>
> Hope this time is better. Thank you!
>
> Yihui
- - - - - - - - - -
  Let 'data' be your four-row data array and 'inp' your three-row input array. You can make use of the 'ismember' function:

 [~,p] = ismember(inp.',data(1:3,:).','rows');
 t = p~=0;
 r = NaN(1,size(inp,2));
 r(t) = data(4,p(t));

The variable 'r' will be your output row vector. It will have NaNs wherever the corresponding three-element column in 'inp' wasn't found in 'data'. If there occurs more than one of the same triple in 'data', the one with the highest column index is selected.

Roger Stafford

Subject: repost problem of find function due to the misaligned text

From: ImageAnalyst

Date: 29 May, 2011 02:02:38

Message: 8 of 9

Okay Roger, if you have a beta version of the Crystal Ball Toolbox
(CBT), I'd like to know how I can get a copy too. Because I couldn't
figure out what the heck he was saying, but apparently you did. In
fact when I programmed up your example according to what I think his
example inputs and outputs are, your code works, well most of the
time. Where it didn't work, I probably didn't understand what he
said, due to not having the CBT. Anyway, here was my test:


function test1()
clc; % Clear the command window.

data = [...
1 4 7
2 5 8
3 6 9
44 74 12 ];

inp = [...
1 4
2 3
3 6 ];
% Yihui wants this:
% 1 4
% 2 5
% 3 6
% 44 74
r1 = roger(data, inp)
% Gives 44 NaN - so didn't work???

% Second example give by Yihui:
data = [1;2;3;4];
inp = [1;2;3];
% Yihui wants 4 as the output.
r2 = roger(data, inp) % works!

% Third example give by Yihui:
data = [1 5 9;2 6 10;3 7 11;4 8 12]
% "I have [1 5 9;2 6 10;3 7 11;4 8 12;]),
% I enter any two rows of first three numbers
% (e.g., [1 9;2 10;3 11])'
% OK, let's pick the 1st and second rows.
inp = [1 9;2 10];
% Yihui wants [4 12] as the output.
% r3 = roger(data, inp)
% Throws exception
% I must not have understood.

% "I have [1 5 9;2 6 10;3 7 11;4 8 12;]),
% I enter any two rows of first three numbers
% Roger runs it through the CBT and figures out
% that "rows" actually means "columns" and
% "numbers" actually means "rows."
% Translation and processing by the CBT then gives
inp = [1 9;2 10;3 11]
% Yihui wants [4 12] as the output.
r4 = roger(data, inp) % Works!

function r = roger(data, inp)
[~,p] = ismember(inp.',data(1:3,:).','rows');
 t = p~=0;
 r = NaN(1,size(inp,2));
 r(t) = data(4,p(t));


%=========================================
Results:

r1 =

    44 NaN


r2 =

     4


data =

     1 5 9
     2 6 10
     3 7 11
     4 8 12


inp =

     1 9
     2 10
     3 11


r4 =

     4 12

Test3 gave an exception when I did what he said, but you figured out
that, to him, "rows" means "columns" and "numbers" means "rows" - I
never would have figured that out! I still don't know what he wanted,
or what you did, but you did get the output he requested, and that's
why I'd also like to get the Crystal Ball Toolbox.

Subject: repost problem of find function due to the misaligned text

From: Roger Stafford

Date: 29 May, 2011 06:28:04

Message: 9 of 9

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <88f6c636-1980-4a6d-9993-b277f456ed23@v10g2000yqn.googlegroups.com>...
> Okay Roger, if you have a beta version of the Crystal Ball Toolbox
> (CBT), I'd like to know how I can get a copy too. Because I couldn't
> figure out what the heck he was saying, but apparently you did. In
> fact when I programmed up your example according to what I think his
> example inputs and outputs are, your code works, well most of the
> time. Where it didn't work, I probably didn't understand what he
> said, due to not having the CBT. Anyway, here was my test:
> ..... (SNIP) .......
> Test3 gave an exception when I did what he said, but you figured out
> that, to him, "rows" means "columns" and "numbers" means "rows" - I
> never would have figured that out! I still don't know what he wanted,
> or what you did, but you did get the output he requested, and that's
> why I'd also like to get the Crystal Ball Toolbox.
- - - - - - - - - - -
  As to that first example I assumed Yihui made a simple error in typing:

"in the first three rows. For instance, I enter
1 4
2 3
3 6"

The only way that made sense to me was that Yihui meant to type '5' instead of '3'. It's the sort of thing I myself do all too often.

  In the third example, as I recall, I simply misread the word 'rows' as meaning columns when Yihui wrote "I enter any two rows". Convenient misreading wasn't it? I think Yihui really did mean columns. It was probably wishful thinking on my part since I too was having a difficult time interpreting the question.

  However, this does bring up an interesting point. Suppose the question really had been any two out of the three matching "coordinates". (They were called that at one point.) That would have made it a much more challenging problem. Choosing any two out of three matching numbers in a row is well beyond ismember's simple matching capabilities. Let's say they have to be presented in the proper order but one of them is missing, and repeating values are to be allowed.

  To put it in more general terms Matlab's hypothetical 'issubseq' function is to test whether certain m-term sequences constitute valid subsequences of any of another set of n-term sequences, where m is less than n. I can think of a way of doing it by brute force with lots of nested for-loops, but no efficient sorting-like methods to reduce the complexity of the algorithm come to mind. Maybe it's a good thing for our peace of mind that this was probably not what was asked.

Roger Stafford

Tags for this Thread

No tags are associated with 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