MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply TodayTo resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Asked by huda nawaf on 14 Sep 2012

Hi I have the result of recursive function is saved in txt file , Below are the results of calling recursive fun. that each call divide the vector into two vectors. the problem is when the function divide the vector , give the new vectors the indices of values in original vectors not the same values.So, I lose the real numbers

for ex. this txtfile

*3 4 5 8 9 10

1 2 3

4 5 6

3

1 2

1 2

3 *

I want code by which can return these vectors

*cluster(1)=[3 4 5 8 9 10]

cluster(2)=[3 4 5]

cluster(3)=[8 9 10]

cluster(4)=[5]

cluster(5)=[3 4]

cluster(6)=[8 9]

cluster(7)=[10]*

thanks in advance

*No products are associated with this question.*

Answer by Jürgen on 14 Sep 2012

I am not sure if I get it , is not so clearly explained but I would work with the length of the OriginalVector of size(OriginalVector,2) and divide it by two ( you will need to use ceil or fix if you have an odd length of course

so in pseudo code

Length= length(V)

HalfLenght=ceil(Length/2)

V1=V(1:HalfLength) V1=V(HalfLength+1:Length)

repeat this until length result is one

or am I mistaken ?

Show 2 older comments

huda nawaf on 14 Sep 2012

Jurgen,

this is not what i mean , maybe I'm not clear

the function is not necessary dividing the vector into equal number of values, and this is not the problem because i displayed the result of my function. my function each time divide the vector into two vectors with different length according to conditions .

this is the input:

x=[3 4 5 8 9 10]; when run the function , the result will be

*1 2 3

4 5 6

3

1 2

1 2

3 *

As you see the function returned the indices of vector's values ,it is returned it in txtfile.

In txtfile ,I saved the input vector (as first line),So can I returend the values instead of indices since I have the original vector?

i want code can map the values in txtfile to vector, where output be

3 4 5

8 9 10

5

3 4

8 9

10

thanks

huda nawaf on 14 Sep 2012

the function divide the vector [3 4 5 8 9 10]at first into two vectors

[3 4 5] and [8 9 10] but it is returning indices of these values[1 2 3] and [4 5 6]

then divide the vector [3 4 5] into two vectors [5] and [3 4], but returns [3] and [1 2]; then divide the vector[ 8 9 10] into two vectors [8 9] and [10],but it returns [1 2] and [3].

these returning indices was stored in txtfile. I need code map these indices into the real values in original vector.

i.e need code map indices into

3 4 5

8 9 10

5

3 4

8 9

10

thanks

Jürgen on 15 Sep 2012

Maybe it is because it is the weekend that I am slow but to be sure I'll try to check if I understand it: in your txt file you have a list of numbers that you read into matlab en put in a vector:x=[3 4 5 8 9 10]; then you divide the vector in parts being: [3 4 5] & [8 9 10]&[5]&[3 4]&[8 9]&10

what do you want to do next? write to the txt?

Answer by Jürgen on 15 Sep 2012

Edited by Jürgen on 15 Sep 2012

I think this code does what you want or no what I think that you want :-) Of course much nicer if you make a function of it X=[3 4 5 8 9 10];

L=length(X); NewL=ceil(L/2); Xnew1=X(1:NewL); Xnew2=X(NewL+1:L);

X=Xnew1; L=length(X); NewL=ceil(L/2); Xnew1=X(1:NewL); Xnew1=X(NewL+1:L);

X=Xnew2; L=length(X); NewL=ceil(L/2); Xnew1=X(1:NewL); Xnew1=X(NewL+1:L);

Show 4 older comments

huda nawaf on 16 Sep 2012

thanks,

input=sim (the adjacacency matrix below)%%%% set of objects ,I want to cluster them.

output=four clusters

C1=[1 2 3],

C2= [11 12 13],

C3=[7 8 9],

C4=[4 5 6 10];

below the main program and the function divide, maybe u finde some variables are not used, that because under processing but it is working correctly.I need from code give me the final clusters.

%%%%main program%%%%%

sim=[0 1 1 0 0 0 0 0 0 1 0 0 0; 1 0 1 0 0 0 0 0 0 0 1 0 0; 1 1 0 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 1 1 0 0 0 1 0 0 0; 0 0 0 1 0 1 0 0 0 0 0 0 0; 0 0 0 1 1 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 1 1 1 0 0 0; 0 0 0 0 0 0 1 0 1 0 0 0 0; 0 0 0 0 0 0 1 1 0 0 0 0 0; 1 0 1 1 0 1 1 0 1 0 0 0 0; 0 1 0 0 0 0 0 0 0 0 0 1 1; 0 0 0 0 0 0 0 0 0 0 1 0 1; 0 0 0 0 0 0 0 0 0 0 1 1 0];

f0=fopen('group_1.txt','a'); [p o]=size(sim);

%%%%compute the degree for each node for i=1:p ind=0; for j=1:o if sim(i,j)~=0 & i~=j ind=ind+1; end deg_nod(i)=ind; end end s1=0; for i=1:p s1=s1+deg_nod(i); end total_edg=s1/2; %%%%compute the modularity matrx for i=1:p for j=1:o B(i,j)=sim(i,j)-((deg_nod(i)*deg_nod(j))/(2*total_edg)); end end %%%compute eignvalue and eignvector [U Beta]=eig(B); %%convert the Beta matrix(eignvalues) into vector k=1; for i=1:p for j=1:o if i==j Beta1(k)=Beta(i,j); k=k+1; end end end %%%sort the Beta1 vector [Beta1 ind]=sort(Beta1,'descend');

if Beta1(1)>0 for j=1:o if U(j,ind(1))>0 s(j)=1; else s(j)=-1; end end

v=s*B*s'; if v>0

%%%divide the eignvector into two groups if sum(s)~=length(s)&& sum(s)~=-length(s)

k=1;k1=1; for j=1:length(s) if s(j)>0 for j1=1:o Grp_1(k,j1)=B(j,j1); trac(k)=j; end k=k+1; else for j2=1:o Grp_2(k1,j2)=B(j,j2); trac1(k1)=j; end k1=k1+1; end end

d=0;d1=0; for i=1:length(trac1) Grp_1(:,trac1(i)-d)=[]; d=d+1; end [m n]=size(Grp_1); for i=1:m f(i)=sum(Grp_1(i,:)); end for i=1:length(trac) Grp_2(:,trac(i)-d1)=[];d1=d1+1; end trac trac1 z=0; for i=1:length(trac) zz(i)=trac(i); end F=Divide(Grp_1,z,trac)

fprintf(f0,'\n'); F1= Divide(Grp_2,z,trac1)

else 'the network is indivisible because s is indivisible' return end else 'the network is indivisible because v<0' Q=(s*B*s')/2 return end else b=0; for z=1:p b=b+Beta1(z)*p; end 'the network is indivisible because Beta1<0' Q=1/2*b; f=fopen('groups.txt') for i=1:5 g=fgetl(f); g1(i)=str2num(g) end

end

%%%%function divide%%%%%%

function [cluster1]=Divide(B,ind1,t) len=length(t); [g1 g2]=size(t); f0=fopen('group_1.txt','a'); for i=1:len

fprintf(f0,'%d ',t(i)); end fprintf(f0,'\n'); [p o]=size(B); row_col=[];

%%%compute eignvalue and eignvector [U Beta]=eig(B); %%convert the Beta matrix(eignvalues) into vector%%convert the Beta matrix(eignvalues) into vector k=1; for i=1:p for j=1:o if i==j Beta1(k)=Beta(i,j); k=k+1; end end end %%%sort the Beta1 vector clear s1; [Beta1 ind]=sort(Beta1,'descend');

if Beta1(1)>0 for j=1:o if U(j,ind(1))>0 s1(j)=1; else s1(j)=-1; end end Q_updat=(s1*B*s1')/2;

v=s1*B*s1'; if v>0 %%%divide the eignvector into two groups clear trac; clear trac0;

k=1;k1=1;%ind2=ind1;ind3=ind1; if sum(s1)~=length(s1)&& sum(s1)~=-length(s1) ind1=ind1+1 for j=1:length(s1)

if s1(j)>0 for j1=1:o GGrp_1(k,j1)=B(j,j1); trac(k)=j; end

dd(k)=t(j); k=k+1; else for j2=1:o GGrp_2(k1,j2)=B(j,j2); trac0(k1)=j;

end dd1(k1)=t(j); k1=k1+1; end end % fprintf(f0,'\n'); fclose(f0);

clear row_col

d=0;d1=0;

for i=1:length(trac0)

GGrp_1(:,trac0(i)-d)=[]; d=d+1; end [m n]=size(GGrp_1); for i=1:m f(i)=sum(GGrp_1(i,:)); end for i=1:m for j=1:n if i~=j B_updat(i,j)=GGrp_1(i,j); else B_updat(i,j)=GGrp_1(i,j)-f(i); end end end

for i=1:length(trac)

GGrp_2(:,trac(i)-d1)=[]; d1=d1+1; end [m1 n1]=size(GGrp_2); for i=1:m1 f1(i)=sum(GGrp_2(i,:)); end for i=1:m1 for j=1:n1 if i~=j B1_updat(i,j)=GGrp_2(i,j); else B1_updat(i,j)=GGrp_2(i,j)-f1(i); end end end

G1=B_updat; % G2=B1_updat; ind1=ind1+2; 'process of dividing Group1'

Divide(G1,ind1,dd); 'process of dividing Group2'

Divide(G2,ind1,dd1)

else 'The group is not division because s1 indivisible'

cluster1=t return end

else 'The group is not division because v<0'

cluster1=t return;

end%%%%%if groups can be divided

else%%% if Beta1 is not poditive will return B %ind1 'The group is not division because Beta1<=0'

cluster1=t return; % end%%%if Beta1

cluster1=t return end

huda nawaf on 16 Sep 2012

I forget tell u the output u will see it in txtfile called group_1.txt

thanks

## 2 Comments

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/48178#comment_99433

Which one of the sets of values are "the results of calling recursive fun"??? All I see are the

input, and thedesiredoutput,notthe results of your recursive function.Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/48178#comment_99437

the first set the returned values(the indices of values) of recursive function, and the second set it is what i need.

the first vector in first set (txtfile)is the input, and the remaining values are the returend indices for the vector after dividing it each call.I saved the input in txtfile too.

thanks