I can not return real values

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

2 Kommentare

Image Analyst
Image Analyst am 14 Sep. 2012
Which one of the sets of values are "the results of calling recursive fun"??? All I see are the input, and the desired output, not the results of your recursive function.
huda nawaf
huda nawaf am 14 Sep. 2012
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

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Jürgen
Jürgen am 14 Sep. 2012

0 Stimmen

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 ?

5 Kommentare

Image Analyst
Image Analyst am 14 Sep. 2012
I'm pretty sure this is related to, and follow up on, his several other, complicated recursion questions. Look up his profile if you're interested in helping further, or await a response here.
huda nawaf
huda nawaf am 14 Sep. 2012
thanks Jurgen, I will try your code
huda nawaf
huda nawaf am 14 Sep. 2012
Bearbeitet: huda nawaf am 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
huda nawaf am 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
Jürgen am 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?

Melden Sie sich an, um zu kommentieren.

Jürgen
Jürgen am 15 Sep. 2012
Bearbeitet: Jürgen am 15 Sep. 2012

0 Stimmen

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);

7 Kommentare

huda nawaf
huda nawaf am 15 Sep. 2012
thanks, Unfotunately , your code just work with this example I gave it
What if the function returned different indices. the function is not necessary divide the vector into two equal size vectors
I will give more details the input is
x=[3 4 5 8 9 10];
I will change the output because I can not guess what is it. when see txtfile I can know what is the output.
the output for ex., I will nark each line for clarifying
L1 1 2 %%%%this is first vector
L2 3 4 5 %%%this is the second vector
L3 1 this is first vector of L1
L4 2 this is second vector of L1
L5 1 2 this is the first vector of L2
L6 3 this is the second vector of L2
this is simple example, in relaity i have long vectors.
this is the output of function, it is returing indices not values
what i need
L1 3 4
L2 8 9 10
L3 3
L4 4
L5 8 9
L6 10
So , the function returen the indices by which I can recover the original values of vector.
thanks
Jürgen
Jürgen am 15 Sep. 2012
I am very sorry, I would like to help you but I don't understand your explaination, and your English is not really helping
"What if the function returned different indices"?? what do you mean?maybe post that function?
"L1 1 2 %%%%this is first vector." Do you mean L1=[1 2]?
"L3 1 this is first vector of L1" How can one elment be the first vector of L1? " this is agian unclear
So , the function returen the indices by which I can recover the original values of vector"
please give us some code and the input vector
or maybe somebodyelse can help you Maybe a final comment: I see that you talk about a function that returns indices adn that you want to have values. I suppose you know that if i is the index X(i) gives the values.
huda nawaf
huda nawaf am 16 Sep. 2012
thanks ,
I solved the problem by modify the code itself not txtfile.
But i have another problem, i think it is simpler than befor. i'm sorry for weak in language, this is why other not always understand me.
Anyway, i applied clustering algorithm, so the result is number of clusters, each cluster has number of objects.
i got these clusters, but because i used recursive algorithm , the function not return just leaf but return all values.
This is the result of running code , the output is txtfile.
the input is :
x=[5 4 6 8 9 10 11 13]
the algorith will cluster this vector , so the output will be 9 10 11 13
10 11
9 13
5 4 8 6
5 4 6
8
in this simple example, i know the clusters of that vector it is cluster1=[10 11]
cluster2=[9 13]
cluster3=[5 4 6]
cluster4=[8]
but if i used large size of data, it is dificult to know the clusters, so i need code to find clusters. the recursive function divide each divisible group into final , then begin with the other
thanks
Jürgen
Jürgen am 16 Sep. 2012
hello Huda,
I am happy that you solved ypur problem. For your new question the problem for me stays the same: I do not understand the problem based on your explantion:"but if i used large size of data, it is dificult to know the clusters, so i need code to find clusters" this is totally unclear to me: post example input, post your code. Input=[ something ]; function Output= NameFucntion(Input) code and show Output
regardsJ
huda nawaf
huda nawaf am 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
huda nawaf am 16 Sep. 2012
I forget tell u the output u will see it in txtfile called group_1.txt
thanks
huda nawaf
huda nawaf am 19 Sep. 2012
hi , many thanks, I have solved the another problem.

Melden Sie sich an, um zu kommentieren.

Tags

Gefragt:

am 14 Sep. 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by