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