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 with text problem

Subject: Matrix with text problem

From: charles atlas

Date: 5 Aug, 2009 14:14:02

Message: 1 of 4

I have a column of data and a matrix with numerical and text data as shown below
data1 =
1 AB Z1
1 BC X2
1 CD Y3
2 DD X 4
2 DE X5
2 FG Z6
3 HI Z6
3 JJ Y7
3 KL X8
4 MN Z9
4 NO Z9
5 OP X10
5 PP X11
5 QR X12
6 RS Y12
6 ST Z12
7 TT Z13
7 TU Y13
7 VX X14
8 XY Z15
8 ZZ Z16

data2 =
1
3
5
6
8

I need to use data2 to take only the information from data1 that I need
the column of numbers from data2 corresponds to the numbers in data2 on the first column that I need. I have a program that does this with numerical data but not for mixed numerical and text data.

Here is what the result should look like

data3 =
1 AB Z1
1 BC X2
1 CD Y3
3 HI Z6
3 JJ Y7
3 KL X8
5 OP X10
5 PP X11
5 QR X12
6 RS Y12
6 ST Z12
8 XY Z15
8 ZZ Z16

I have no idea how to do this with text data.
thanks in advance for any help
Charlie--

Subject: Matrix with text problem

From: Rune Allnor

Date: 5 Aug, 2009 14:22:53

Message: 2 of 4

On 5 Aug, 16:14, "charles atlas" <mufasa1234...@yahoo.com> wrote:
> I have a column of data and a matrix with numerical and text data as shown below
...
> I have no idea how to do this with text data.
> thanks in advance for any help

Use an array of structs, where one field
represents the numbers on a numeric format.
Then use your existing program.

Or sort both data sets, and scan data1 for
lines that contain numbers from data2.

Rune

Subject: Matrix with text problem

From: Andy

Date: 5 Aug, 2009 14:40:23

Message: 3 of 4

"charles atlas" <mufasa1234555@yahoo.com> wrote in message <h5c43a$q4h$1@fred.mathworks.com>...
> I have a column of data and a matrix with numerical and text data as shown below
> data1 =
> 1 AB Z1
> 1 BC X2
> 1 CD Y3
> 2 DD X 4
> 2 DE X5
> 2 FG Z6
> 3 HI Z6
> 3 JJ Y7
> 3 KL X8
> 4 MN Z9
> 4 NO Z9
> 5 OP X10
> 5 PP X11
> 5 QR X12
> 6 RS Y12
> 6 ST Z12
> 7 TT Z13
> 7 TU Y13
> 7 VX X14
> 8 XY Z15
> 8 ZZ Z16
>
> data2 =
> 1
> 3
> 5
> 6
> 8
>
> I need to use data2 to take only the information from data1 that I need
> the column of numbers from data2 corresponds to the numbers in data2 on the first column that I need. I have a program that does this with numerical data but not for mixed numerical and text data.
>
> Here is what the result should look like
>
> data3 =
> 1 AB Z1
> 1 BC X2
> 1 CD Y3
> 3 HI Z6
> 3 JJ Y7
> 3 KL X8
> 5 OP X10
> 5 PP X11
> 5 QR X12
> 6 RS Y12
> 6 ST Z12
> 8 XY Z15
> 8 ZZ Z16
>
> I have no idea how to do this with text data.
> thanks in advance for any help
> Charlie--

% Try this
data1 ={
    1 'AB' 'Z1';
    1 'BC' 'X2';
    1 'CD' 'Y3';
    2 'DD' 'X4';
    2 'DE' 'X5';
    2 'FG' 'Z6';
    3 'HI' 'Z6';
    3 'JJ' 'Y7';
    3 'KL' 'X8';
    4 'MN' 'Z9';
    4 'NO' 'Z9';
    5 'OP' 'X10';
    5 'PP' 'X11';
    5 'QR' 'X12';
    6 'RS' 'Y12';
    6 'ST' 'Z12';
    7 'TT' 'Z13';
    7 'TU' 'Y13';
    7 'VX' 'X14';
    8 'XY' 'Z15';
    8 'ZZ' 'Z16'};

data2 =[
    1;
    3;
    5;
    6;
    8];
[tf loc]=ismember(cell2mat(data1(:,1)),data2);
data3=data1(logical(loc),:);
disp(data3)
%{
% output
    [1] 'AB' 'Z1'
    [1] 'BC' 'X2'
    [1] 'CD' 'Y3'
    [3] 'HI' 'Z6'
    [3] 'JJ' 'Y7'
    [3] 'KL' 'X8'
    [5] 'OP' 'X10'
    [5] 'PP' 'X11'
    [5] 'QR' 'X12'
    [6] 'RS' 'Y12'
    [6] 'ST' 'Z12'
    [8] 'XY' 'Z15'
    [8] 'ZZ' 'Z16'
%}

Subject: Matrix with text problem

From: Greg

Date: 5 Aug, 2009 15:49:02

Message: 4 of 4

"charles atlas" <mufasa1234555@yahoo.com> wrote in message <h5c43a$q4h$1@fred.mathworks.com>...
> I have a column of data and a matrix with numerical and text data as shown below
> data1 =
> 1 AB Z1
> 1 BC X2
> 1 CD Y3
> 2 DD X 4
> 2 DE X5
> 2 FG Z6
> 3 HI Z6
> 3 JJ Y7
> 3 KL X8
> 4 MN Z9
> 4 NO Z9
> 5 OP X10
> 5 PP X11
> 5 QR X12
> 6 RS Y12
> 6 ST Z12
> 7 TT Z13
> 7 TU Y13
> 7 VX X14
> 8 XY Z15
> 8 ZZ Z16
>
> data2 =
> 1
> 3
> 5
> 6
> 8
>
> I need to use data2 to take only the information from data1 that I need
> the column of numbers from data2 corresponds to the numbers in data2 on the first column that I need. I have a program that does this with numerical data but not for mixed numerical and text data.
>
> Here is what the result should look like
>
> data3 =
> 1 AB Z1
> 1 BC X2
> 1 CD Y3
> 3 HI Z6
> 3 JJ Y7
> 3 KL X8
> 5 OP X10
> 5 PP X11
> 5 QR X12
> 6 RS Y12
> 6 ST Z12
> 8 XY Z15
> 8 ZZ Z16
>
> I have no idea how to do this with text data.
> thanks in advance for any help
> Charlie--

If you have the Statistics Toolbox and a more recent version of MATLAB you can use the Dataset Array to create matrices of mixed data types. From how you described your use case, this seems a perfect application for the Dataset Array.

For more information about the Dataset Array, view the documentation at the following link.

http://www.mathworks.com/access/helpdesk/help/toolbox/stats/bqziht7-1.html#bqzihxq

Cheers,
-Greg

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