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:
help needed wit array~~

Subject: help needed wit array~~

From: si yeol Shin

Date: 1 Oct, 2010 07:55:23

Message: 1 of 11

i have a txt file like this...

1 20
1 21
1 30
2 4
2 29
3 28
3 40
3 1
3 4
4 43
4 10
4 4

and i wanna convert it like below...

20 4 28 43
21 29 40 10
30 1 4
              4

what i wanna say is...
at first i divide the values of column 1.
1 2 3 4
1 2 3 4
1 3 4
         3
and then change the values as follow values of column 2.
so the result is..

20 4 28 43
21 29 40 10
30 1 4
              4
i wonder if u didn't understand what i mean...
plz anybody knows help me.. :)

Subject: help needed wit array~~

From: Jos (10584)

Date: 1 Oct, 2010 08:30:25

Message: 2 of 11

"si yeol Shin" <siyory@kookmin.ac.kr> wrote in message <i8445b$dhc$1@fred.mathworks.com>...
> i have a txt file like this...
>
> 1 20
> 1 21
> 1 30
> 2 4
> 2 29
> 3 28
> 3 40
> 3 1
> 3 4
> 4 43
> 4 10
> 4 4
>
> and i wanna convert it like below...
>
> 20 4 28 43
> 21 29 40 10
> 30 1 4
> 4
>
> what i wanna say is...
> at first i divide the values of column 1.
> 1 2 3 4
> 1 2 3 4
> 1 3 4
> 3
> and then change the values as follow values of column 2.
> so the result is..
>
> 20 4 28 43
> 21 29 40 10
> 30 1 4
> 4
> i wonder if u didn't understand what i mean...
> plz anybody knows help me.. :)

In this particular example, as simple RESHAPE of the second column would do since the values of 1-4 are continuous and the number of elements is a multiplication of 3:

reshape(M(:,2),3,[])

Otherwise, I think I do not understand what you mean ... Perhaps you can give a more general example?

Jos

Subject: help needed wit array~~

From: si yeol Shin

Date: 1 Oct, 2010 08:57:06

Message: 3 of 11

"Jos (10584) " <#10584@fileexchange.com> wrote in message <i84671$194$1@fred.mathworks.com>...
> "si yeol Shin" <siyory@kookmin.ac.kr> wrote in message <i8445b$dhc$1@fred.mathworks.com>...
> > i have a txt file like this...
> >
> > 1 20
> > 1 21
> > 1 30
> > 2 4
> > 2 29
> > 3 28
> > 3 40
> > 3 1
> > 3 4
> > 4 43
> > 4 10
> > 4 4
> >
> > and i wanna convert it like below...
> >
> > 20 4 28 43
> > 21 29 40 10
> > 30 1 4
> > 4
> >
> > what i wanna say is...
> > at first i divide the values of column 1.
> > 1 2 3 4
> > 1 2 3 4
> > 1 3 4
> > 3
> > and then change the values as follow values of column 2.
> > so the result is..
> >
> > 20 4 28 43
> > 21 29 40 10
> > 30 1 4
> > 4
> > i wonder if u didn't understand what i mean...
> > plz anybody knows help me.. :)
>
> In this particular example, as simple RESHAPE of the second column would do since the values of 1-4 are continuous and the number of elements is a multiplication of 3:
>
> reshape(M(:,2),3,[])
>
> Otherwise, I think I do not understand what you mean ... Perhaps you can give a more general example?
>
> Jos

thank u for replying .... :)
but i think u didn't understand exactly what i mean...
the array what i wrote on this page was broken.
and u can c the original array on reply window.
when u click the reply button.
so it is not the case of reshape...i think...

Subject: help needed wit array~~

From: Ross W

Date: 1 Oct, 2010 10:09:06

Message: 4 of 11

"si yeol Shin" <siyory@kookmin.ac.kr> wrote in message <i8445b$dhc$1@fred.mathworks.com>...
> i have a txt file like this...
>
> 1 20
> 1 21
> 1 30
> 2 4
> 2 29
> 3 28
> 3 40
> 3 1
> 3 4
> 4 43
> 4 10
> 4 4
>
> and i wanna convert it like below...
>
> 20 4 28 43
> 21 29 40 10
> 30 1 4
> 4
>
> what i wanna say is...
> at first i divide the values of column 1.
> 1 2 3 4
> 1 2 3 4
> 1 3 4
> 3
> and then change the values as follow values of column 2.
> so the result is..
>
> 20 4 28 43
> 21 29 40 10
> 30 1 4
> 4
> i wonder if u didn't understand what i mean...
> plz anybody knows help me.. :)

If the first column indicates the row, and is always increasing when it changes, then the following might be a good start:

data= [
1 20
 1 21
 1 30
 2 4
 2 29
 3 28
 3 40
 3 1
 3 4
 4 43
 4 10
 4 4
];

n=size(data,1); %how many lines of data

pos=[0; find(diff(data(:,1))~=0)]; %where the first column changes

i1=(1:n)'-pos(data(:,1)); %which column

i2=data(:,1); %which row

result=zeros(max(i1),max(i2)); %make a complete array

result(sub2ind(size(result),i1,i2))=data(:,2); %put in results where we have them

With this code there will be zeros in the places you left empty in your example. You can change to suit your needs.

Ross

Subject: help needed wit array~~

From: ImageAnalyst

Date: 1 Oct, 2010 10:26:29

Message: 5 of 11

On Oct 1, 4:57 am, "si yeol Shin" <siy...@kookmin.ac.kr> wrote:
> thank u for replying ....    :)
> but i think u didn't understand exactly what i mean...
> the array what i wrote on this page was broken.
> and u can c the original array on reply window.
> when u click the reply button.
> so it is not the case of reshape...i think...
---------------------------------------------------------------------------------------------------------
I don't understand either. I thought of reshape at first also. What
is a "broken" matrix? And why do you want one? A matrix has to be
rectangular. You can't have just nothing, or null, or blank empty
spaces at positions (3,2), (4,1), (4,2), and (4,4). There HAS to be
something there, either a zero or a NaN or *something* but not just
nothing. So, for example, what do you want in row 3 between the 30
and the 1? There *needs* to be something there in that spot.

Subject: help needed wit array~~

From: si yeol Shin

Date: 4 Oct, 2010 02:59:04

Message: 6 of 11

thank you everyone~~~~
i almost understand the algorism that how to solve my case~ :)
but ...
i've got a new problem that is a using memory error.
i was using Matlab 2007 when i got the error message.
and Matlab got an upgrade to version 2010 to solve the problem.
but it made different problem that is
'Maximum variable size allowed by the program is exceeded.'

so i looked for all the comments to solve it.
but it's too hard for me because i'm a beginner for Matlab.
i have a txt file that is used for my project, and contains many columns and too many rows. is there any solution for this case?

Subject: help needed wit array~~

From: si yeol Shin

Date: 4 Oct, 2010 03:26:04

Message: 7 of 11

data= [
1 20
 1 21
 1 30
 2 4
 2 29
 3 28
 3 40
 3 1
 3 4
 4 43
 4 10
 4 4
];

n=size(data,1); %how many lines of data

pos=[0; find(diff(data(:,1))~=0)]; %where the first column changes

i1=(1:n)'-pos(data(:,1)); %which column

i2=data(:,1); %which row

result=zeros(max(i1),max(i2)); %make a complete array

result(sub2ind(size(result),i1,i2))=data(:,2); %put in results where we have them

With this code there will be zeros in the places you left empty in your example. You can change to suit your needs.

Ross
----------------------------------------------------------------------------------------------------------------
'i1=(1:n)'-pos(data(:,1)); %which column'
and this part doesn' work. make error 'Index exceeds matrix dimensions.'

Subject: help needed wit array~~

From: Ross W

Date: 4 Oct, 2010 04:12:04

Message: 8 of 11

"si yeol Shin" <siyory@kookmin.ac.kr> wrote in message <i8bhgb$asd$1@fred.mathworks.com>...
> data= [
> 1 20
> 1 21
> 1 30
> 2 4
> 2 29
> 3 28
> 3 40
> 3 1
> 3 4
> 4 43
> 4 10
> 4 4
> ];
>
> n=size(data,1); %how many lines of data
>
> pos=[0; find(diff(data(:,1))~=0)]; %where the first column changes
>
> i1=(1:n)'-pos(data(:,1)); %which column
>
> i2=data(:,1); %which row
>
> result=zeros(max(i1),max(i2)); %make a complete array
>
> result(sub2ind(size(result),i1,i2))=data(:,2); %put in results where we have them
>
> With this code there will be zeros in the places you left empty in your example. You can change to suit your needs.
>
> Ross
> ----------------------------------------------------------------------------------------------------------------
> 'i1=(1:n)'-pos(data(:,1)); %which column'
> and this part doesn' work. make error 'Index exceeds matrix dimensions.'

On my version of Matlab, it runs fine.
Are you using a different data array?

After you get the error, what results do you get from these commands?

min(data(:,1))
max(data(:,1))
size(pos)

Ross

Subject: help needed wit array~~

From: si yeol Shin

Date: 4 Oct, 2010 05:39:35

Message: 9 of 11

yes....
i tried to apply it to my example data.
so it made the problem. i didn't know that.
there are 30001-30080 on the column 1 in the example data, and the pattern of each number's quantity is the same as before i wrote example.
and the column 2 has values as many as the values on the column 1.

let me explain a original data that i have has more values than example data.

Subject: help needed wit array~~

From: Ross W

Date: 4 Oct, 2010 06:18:03

Message: 10 of 11

"si yeol Shin" <siyory@kookmin.ac.kr> wrote in message <i8bpan$42r$1@fred.mathworks.com>...
> yes....
> i tried to apply it to my example data.
> so it made the problem. i didn't know that.
> there are 30001-30080 on the column 1 in the example data, and the pattern of each number's quantity is the same as before i wrote example.
> and the column 2 has values as many as the values on the column 1.
>
> let me explain a original data that i have has more values than example data.

Assuming you still have the data sorted by value in column 1 ...

clear all
data= [
 30001 20
 30001 21
 30001 30
 30002 4
 30002 29
 30003 28
 30003 40
 30003 1
 30003 4
 30004 43
 30004 10
 30004 4
];
% more like your data

n=size(data,1); %how many lines of data

change_data=diff(data(:,1))~=0; %=1 where data value changes

pos=[0; find(change_data)]; %where the first column changes

i1=(1:n)'-pos(cumsum([1; change_data])); %which column

i2=cumsum([1; change_data]); %which row

result=zeros(max(i1),max(i2)); %make a complete array

result(sub2ind(size(result),i1,i2))=data(:,2); %put in results where we have them
>> result

result =

    20 4 28 43
    21 29 40 10
    30 0 1 4
     0 0 4 0


Ross

Subject: help needed wit array~~

From: si yeol Shin

Date: 4 Oct, 2010 06:54:08

Message: 11 of 11

thank you so much~~
it works now... :)
and i could be back to ask something again~
thanks a lot~~

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