Click to See Complete Forum and Search --> : can someone help me convert/understand this code (matlab to c)


ankursaxena
February 16th, 2004, 12:00 PM
Hi I am very new to matlab and have no deep understanding of it. Is it possible to someone to convert this code to C/C++ using the matlab tool(i dont have one) or may be explain this code to me a little so that I can work on it myself and convert to c++.

I would be really greatful if you could please, thanx you so much.

Here is the code.




% this algorithm follows the algorithm of the first method except the
% powers are finally divided by the efficiencies.

[data] = textread('input_4000_00.txt','','delimiter',' ');

eff_flag = data(1,2);
data_size = size(data);
eff = data(3:data_size(1,1),2)';

[d_acc orig_pos] = sort(data(3:data_size(1,1),3));
d_acc = d_acc(2:length(d_acc))';


d = zeros(1,length(d_acc));
d(1,1) = d_acc(1,1);
for i = 2:length(d_acc)
d(1,i) = d_acc(1,i) - d_acc(i-1);
end

node_id = 0; %any node ID between 0 and max number (399/3999)

for i=1:length(orig_pos)
if (orig_pos(i) == (node_id + 1))
pri_source = i;
end
end
power_matrix = zeros(1,length(d)+1);

if (pri_source > 1 && pri_source < length(orig_pos))


index_right = pri_source;
index_left = pri_source;

for j = pri_source+1:length(d)
if (d(index_right) < (d(j) - (d_acc(j-1) - d_acc(index_right - 1))))
index_right = j;
end
end

for j = pri_source-1:-1:2
if (d(index_left -1) < (d(j-1) - (d_acc(index_left - 1) - d_acc(j-1))))
index_left = j;
end
end


distance_fr_src_left = d_acc(pri_source-1) - d_acc(index_left - 1);
distance_rem_left = d(index_left - 1) - distance_fr_src_left;

eff_distance_covered_left = 0;
if (distance_rem_left > 0)
for i=pri_source:length(d)
if((d_acc(i) - d_acc(pri_source-1)) > distance_rem_left)
break;
end
eff_distance_covered_left = d_acc(i) - d_acc(pri_source-1);
end
end


distance_fr_src_right = d_acc(index_right-1) - d_acc(pri_source - 1);
distance_rem_right = d(index_right) - distance_fr_src_right;

eff_distance_covered_right = 0;

if (distance_rem_right > 0)
for i=pri_source-2:-1:0

if (i > 0)
tot_dist = d_acc(pri_source - 1) - d_acc(i);
else
tot_dist = d_acc(pri_source - 1);
end

if (tot_dist > distance_rem_right)
break;
end
eff_distance_covered_right = tot_dist;
end
end

if (eff_distance_covered_right > eff_distance_covered_left)
power_matrix(1,pri_source) = power(d(pri_source),2);
dir = 1;

elseif (eff_distance_covered_right < eff_distance_covered_left)
power_matrix(1,pri_source) = power(d(pri_source-1),2);
dir = -1;

elseif ((eff_distance_covered_right == eff_distance_covered_left) && (eff_distance_covered_right > 0))
if (d(pri_source -1) < d(pri_source))
dir = -1;
else
dir = 1;
end
power_matrix(1,pri_source) = power(min(d(pri_source -1:pri_source)),2);

else
power_matrix(1,pri_source) = power(max(d(pri_source -1:pri_source)),2);
dir = 1;
end


if (dir == 1)

for sec_source = pri_source + 1:length(d)
power_matrix(1,sec_source) = power(d(sec_source),2);
end

for sec_source = pri_source -1:-1:2
flag = 0;
for check_source=pri_source:index_right
if (sec_source > 2)
dist = d_acc(check_source - 1) - d_acc(sec_source - 2);
else
dist = d_acc(check_source - 1);
end
if (power_matrix(1,check_source) >= power(dist,2))
flag = 1;
break;
end
end
if (flag == 0)
power_matrix(1,sec_source) = power(d(sec_source-1),2);
end

end

else
for sec_source= pri_source -1:-1:2
power_matrix(1,sec_source) = power(d(sec_source-1),2);
end

for sec_source=pri_source+1:length(d)
flag = 0;
for check_source = pri_source:-1:index_left
dist = d_acc(sec_source) - d_acc(check_source - 1);
if (power_matrix(1,check_source) >= power(dist,2))
flag = 1;
break;
end
end
if (flag == 0)
power_matrix(1,sec_source) = power(d(sec_source),2);
end

end

end

end

%setup the power profile for the first and last node

if (pri_source == 1)
for i = 1:length(d)
power_matrix(1,i) = power(d(i),2);
end
end

if (pri_source == length(orig_pos))
for i = 1:length(d)
power_matrix(1,i+1) = power(d(i),2);
end
end

%divide the power matrix by the efficiencies of the respective nodes.
power_matrix(1,:) = power_matrix(1,:) ./ eff;

orig_power_matrix = zeros(1,length(d)+1);

for j = 1:length(orig_pos)
orig_power_matrix(1,orig_pos(j)) = power_matrix(1,j);
end

fid = fopen('third_output_40000_00.txt','w');

for i = 1:length(orig_pos)
fprintf(fid,'%d %f\n',i-1,orig_power_matrix(i));
end

fclose(fid);





The input text file looks like this.

5 0
4
0 1 4 0
1 1 0 0
2 1 2 0
3 1 5 0
4 1 8 0

Thanks,
Nittin

ankursaxena
February 16th, 2004, 12:20 PM
Hi,
I might be needing a little more help off and on. I tried to convert it myself. I have gotten not very far and got stuck. Let me start off.

[data] = textread...
This just reads the file into a matrix called data.

eff_flag = ..
This gets the 1st elem from the 2nd col from data

data_size = size(..
This gets the matrix size in our eg. 7x4 i guess

eff = data(3:data_size(...)'
This gets the 2nd col starting from elem 3 downto the last elem and transpose it to make it 1 row and so many cols. in our eg eff = [1 1 1 1] I guess

[d_acc orig_pos] = sort(data(3:data_size(1,1), 3));
This is where I am stuck now i dont know what this would do.
I know It will sort the the 3rd col( I am guessing ) but I dont know what goes into d_acc and what goes into orig_pos. I should assume that the 3rd col should be orig_pos since its the co-ordinate, but from this syntax I dont know how.
the text file input is



"# of elem" "some flag"
"some flag"
"col for ids" "col for eff" "col for co-ordinate" "junk col"




So this is how the input information is interpreted.

d = zeros(1, len(d_acc))
This should create a matrix of 1 row and len of d_acc cols and fill it with 0.

d(1,1) = d_acc(2:length(d_acc))
I am also not sure what this does, since I dont know what info is in d_acc, but I am assuming this gives me the largest dimenssion of the d_acc matrix which should be a 1 col multiple rows matrix = how many rows in d_acc = d(1,1)

for i=2:len..
This will fun a loop from 2 to len of d_acc

d(1,i) = d_acc(1, i) - d_acc(i-1);
I totaly dont understand this, i dont know what d_acc(i-1) should return. I guess I should stop here since I am getting lost too often, if I understand the code a little more then I will be able to go forward i guess.

Thank for your help, i am really running out of time, I have to get this done by wed and I hope i can.

I really appreciate your help.

Thanks again.
Nittin.