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:
Divide words in groups by theirs frequency mean

Subject: Divide words in groups by theirs frequency mean

From: Andrea

Date: 5 Mar, 2012 17:18:12

Message: 1 of 8

Dear all

I have a certain set of word in a txt file and they looks like this

ACADEMY 24 7
ACCIDENT 33 8

The first column is the word the second is the frequency and the third is the number of letters.

with:

fid=fopen('words.txt')
A=textscan(fid,'%s %s %s')

I got the matrix A that contains 3 cell, then with:

tmp1=A{1}(1) I get the first word - but this is a cell
So I do tmp1{1} to get first word
Then I do tmp2=A{2}(1) to get frequency (the second number)
and then I do str2num(tmp2{1}) to get this as a number (rather than a string).

I have a list of 288 words and I want to divide them in 16 groups of 18 words each,
but the problem is that each group must have the same frequency mean of the others.

I tried many loops but I can't get the result…

Could someone give me a hand?

Thank you very much

Andrea

Subject: Divide words in groups by theirs frequency mean

From: TideMan

Date: 5 Mar, 2012 19:10:43

Message: 2 of 8

On Tuesday, March 6, 2012 6:18:12 AM UTC+13, Andrea wrote:
> Dear all
>
> I have a certain set of word in a txt file and they looks like this
>
> ACADEMY 24 7
> ACCIDENT 33 8
>
> The first column is the word the second is the frequency and the third is the number of letters.
>
> with:
>
> fid=fopen('words.txt')
> A=textscan(fid,'%s %s %s')
>
> I got the matrix A that contains 3 cell, then with:
>
> tmp1=A{1}(1) I get the first word - but this is a cell
> So I do tmp1{1} to get first word
> Then I do tmp2=A{2}(1) to get frequency (the second number)
> and then I do str2num(tmp2{1}) to get this as a number (rather than a string).
>
> I have a list of 288 words and I want to divide them in 16 groups of 18 words each,
> but the problem is that each group must have the same frequency mean of the others.
>
> I tried many loops but I can't get the result…
>
> Could someone give me a hand?
>
> Thank you very much
>
> Andrea

Why are you reading in the numbers as text?
Try this:
A=textscan(fid,'%s%f%f');
words=A{1};
freq=A{2};
nlet=A{3};

Now you can order the data into increasing freq using sort, but you'll need to use the 2nd output argument of sort to also order words and nlet. Once you've sorted them, you can use reshape to get 16 groups of 18 words.

Subject: Divide words in groups by theirs frequency mean

From: Andrea

Date: 5 Mar, 2012 19:59:12

Message: 3 of 8

TideMan <mulgor@gmail.com> wrote in message <28028126.827.1330974643179.JavaMail.geo-discussion-forums@yner4>...

>
> Why are you reading in the numbers as text?
> Try this:
> A=textscan(fid,'%s%f%f');
> words=A{1};
> freq=A{2};
> nlet=A{3};
>
> Now you can order the data into increasing freq using sort, but you'll need to use the 2nd output argument of sort to also order words and nlet. Once you've sorted them, you can use reshape to get 16 groups of 18 words.

Hi TideMan

Even in that case the problem still holds with the word and in top of that I need to keep the relation between each word and its "frequency/number of letter" because, as I wrote before (but maybe was not clear) I need to build each group in a way that it has the same frequency mean as all the other groups.

Thank you very much

Andrea

Subject: Divide words in groups by theirs frequency mean

From: TideMan

Date: 5 Mar, 2012 20:29:00

Message: 4 of 8

You didn't read my post.
[freq1,indx]=sort(freq);
words1=words(indx);
nlet1=nlet(indx);

Now, everything is re-ordered into ascending order of freq.

Subject: Divide words in groups by theirs frequency mean

From: Andrea

Date: 5 Mar, 2012 21:16:10

Message: 5 of 8

TideMan <mulgor@gmail.com> wrote in message <24067323.1218.1330979340120.JavaMail.geo-discussion-forums@ynll40>...
> You didn't read my post.
> [freq1,indx]=sort(freq);
> words1=words(indx);
> nlet1=nlet(indx);
>
> Now, everything is re-ordered into ascending order of freq.

You are right TideMan, but I still have the problem to divide them in groups with the same mean, and I don't know how to do this without losing the relationship. Moreover, once they are ordered can I put them in a matrix? because I need to extract them one by one to be presented on a screen - I have e specific vector that goes from 1 to 288 and each position correspond to one words (is a psychological experiment)

thank you

Andrea

Subject: Divide words in groups by theirs frequency mean

From: TideMan

Date: 5 Mar, 2012 22:13:50

Message: 6 of 8

I'm not sure how you're going to get the mean of each group exactly equal, but here's a way to get them approximately equal - I'll show just the freq1 vector, but you must repeat exactly the same commands for the other two vectors:
freq1=freq1(:); % This ensures we have a column vector
freq2=reshape(freq1,18,16);
% Flip each alternate row
freq2(2:2:end,:)=fliplr(freq2(2:2:end,:));
% Check the means
mean(freq2)

Subject: Divide words in groups by theirs frequency mean

From: Andrea

Date: 5 Mar, 2012 23:08:12

Message: 7 of 8

TideMan <mulgor@gmail.com> wrote in message <32623445.769.1330985630984.JavaMail.geo-discussion-forums@ynhs12>...
> I'm not sure how you're going to get the mean of each group exactly equal, but here's a way to get them approximately equal - I'll show just the freq1 vector, but you must repeat exactly the same commands for the other two vectors:
> freq1=freq1(:); % This ensures we have a column vector
> freq2=reshape(freq1,18,16);
> % Flip each alternate row
> freq2(2:2:end,:)=fliplr(freq2(2:2:end,:));
> % Check the means
> mean(freq2)

In doing freq2=reshape(freq1,18,16)
I got:

??? Error using ==> reshape
To RESHAPE the number of elements must not change.

and freq1 is a column vector

Subject: Divide words in groups by theirs frequency mean

From: Andrea

Date: 5 Mar, 2012 23:21:11

Message: 8 of 8

"Andrea" wrote in message <jj3h0s$fo5$1@newscl01ah.mathworks.com>...
> TideMan <mulgor@gmail.com> wrote in message <32623445.769.1330985630984.JavaMail.geo-discussion-forums@ynhs12>...
> > I'm not sure how you're going to get the mean of each group exactly equal, but here's a way to get them approximately equal - I'll show just the freq1 vector, but you must repeat exactly the same commands for the other two vectors:
> > freq1=freq1(:); % This ensures we have a column vector
> > freq2=reshape(freq1,18,16);
> > % Flip each alternate row
> > freq2(2:2:end,:)=fliplr(freq2(2:2:end,:));
> > % Check the means
> > mean(freq2)
>
> In doing freq2=reshape(freq1,18,16)
> I got:
>
> ??? Error using ==> reshape
> To RESHAPE the number of elements must not change.
>
> and freq1 is a column vector

Sorry TideMan I understand what you meant , but I realised that there is a big problem because, as I said I need each group with the same mean, but at the same time I've just realised that I also need that each group has at least 1 word from the 10-20 frequency range, 1 from the 20-30, and so on...

I need to think about that carefully.
Anyway, thank you very much for your help.

Andrea

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