I am working with simulation of wireless sensor networks in matlab.
I have a 200*200 by field in which 100 sensor nodes have been plotted randomly. Each node has an associated load value with it. I have to place charging stations in this field. I am trying to divide this square recursively as long as I do not found a small sub-square in which I can place only one charging station. Here is the code I wrote to divide the square recursively and count number of stations that can be placed in a subsquare:
%Inputs to the function
%numstations - No. of stations to be placed = 10
%boundCoords - A 2*2 matrix with min and max coordinates of square . e.g [0 0;200 200]
% sensors - A 100*3 matrix for nodes with 1st column as randomly generated 100 x-coordinates,
%second column as randomly generated 100 y-coordinates,
%third column as corresponding load of each node (can be random)
function stationPoss = deploy(numStations, boundCoords)
global sensors;
centerCoord = mean(boundCoords, 1);
numSensors = size(sensors, 1);
sumQuadLoad = zeros(1, 4);
for i = 1:numSensors
if sensors(i, 1) < boundCoords(2, 1) && sensors(i, 2) < boundCoords(2, 2)...
&& sensors(i, 1) > boundCoords(1, 1) && sensors(i, 2) > boundCoords(1, 2)
isIn34Quads = sensors(i, 1) > centerCoord(1); % N
isIn24Quads = sensors(i, 2) > centerCoord(2);
biQuadIndex = [isIn34Quads, isIn24Quads];
quadIndex = bi2de(biQuadIndex) + 1;
sumQuadLoad(quadIndex) = sumQuadLoad(quadIndex) + sensors(i, 3);
end
end
if numStations == 1
[maxQuadLoad, quad] = max(sumQuadLoad); %#ok<ASGLU>
delta = (centerCoord - boundCoords(1, :)) .* de2bi(quad - 1);
assoQuadCoords = [boundCoords(1, :); centerCoord] + repmat(delta, 2, 1);
stationPoss = mean(assoQuadCoords, 1);
else
sumLoad = sum(sumQuadLoad);
quadNumStations = zeros(1, 4);
for i = 1:3
if sumQuadLoad(i) == 0
quadNumStations(i) = 0;
else
quadNumStations(i) = floor(numStations * sumQuadLoad(i) / sumLoad);
end
end
quadNumStations(4) = numStations - sum(quadNumStations);
stationPoss = zeros(numStations, 2);
for i = 1:4
delta = (centerCoord - boundCoords(1, :)) .* de2bi(i - 1);
newBoundCoords = [boundCoords(1, :); centerCoord] + repmat(delta, 2, 1);
if quadNumStations(i) ~= 0
indexRange = sum(quadNumStations(1:i-1)) + (1:quadNumStations(i));
stationPoss(indexRange, :) = deploy(quadNumStations(i), newBoundCoords);
end
end
end
The problem is while trying to run this code with numStations=2 it works fine and with numStations=3 it sometimes crashes. For numStation > 3 it almost always crashes.
I tried to come up with a non-recursive way to write this function but wasn't able to.
Will anyone please help me to figure out the crash problem or in writing non recursive solution to the above function. I have already tried increasing the recursion limit.
I am Trying to convert a MATLAB code to C++ using MATLAB coder but this error apears:
Error indenting generated C code
The error points to the name of the function itself and has no more explanations in it. can someone tell me what is this error?
here is the function i want to conver:
function [Report_Clustered,ClusterCounter_new]=InitClusterGenerator_test(Report_In,~,FreqEpsilon,DegreeEpsilon,~,ClusterCounter_old, BlockCount, Report_old)
Report_M = zeros(size(Report_In,1),size(Report_In,2),4);
for i=1:size(Report_In,1)
for j=1:size(Report_In,2)
Report_M(i,j,1)=Report_In(i,j,1);
Report_M(i,j,2)=Report_In(i,j,2);
Report_M(i,j,3)=0; % Cluster number that the point belongs to.
Report_M(i,j,4)=0;
Report_In{i,j}
end
end
ClusterCounter = 0;
for i=1:size(Report_M,1)
for j=1:size(Report_M,2)
if (Report_M(i,j,3) == 0)
ClusterCounter = ClusterCounter + 1;
Report_M(i,j,3) = ClusterCounter;
for ii=1:size(Report_M,1)
for jj=1:size(Report_M,2)
if (Report_M(ii,jj,3) == 0)
if (abs(Report_M(i,j,1)-Report_M(ii,jj,1))<FreqEpsilon &&...
(abs(Report_M(i,j,2)-Report_M(ii,jj,2)) <DegreeEpsilon ||...
abs(-360 + Report_M(i,j,2)-Report_M(ii,jj,2)) <DegreeEpsilon ||...
abs(360 + Report_M(i,j,2)-Report_M(ii,jj,2)) <DegreeEpsilon))
Report_M(ii,jj,3) = ClusterCounter;
end
end
end
end
end
end
end
if (BlockCount> 20 && ClusterCounter<4)
warning = 1;
end
ClusterCounter_new = ClusterCounter;
%clear Report_new;
flag = 0;
Report_new = zeros(ClusterCounter,size (Report_M, 2),4);
index = zeros(1, ClusterCounter_new);
for i = 1: size (Report_M, 1)
for j = 1: size (Report_M, 2)
for k = 1: ClusterCounter_new
if (Report_M(i,j,3) == k)
index(1,k) = index(1,k) + 1;
Report_new(k,index(1,k), 1:3) = Report_M(i,j,1:3);
flag = flag + 1;
end
end
end
end
for j = 1: size (Report_new, 2)
for i = 1: size (Report_new, 1)
if (Report_new(i,j,1) == 0)
Report_new(i,j,1:3) = Report_new(i,1,1:3);
end
end
end
%Report_new = Report;
MedoidF_old = zeros(1, size(Report_old,1));
MedoidA_old = zeros(1, size(Report_old,1));
for i=1:size(Report_old,1)
SumF = 0;
SumA = 0;
MinAngle = 361;
MaxAngle = -1;
for j=1:size(Report_old,2)
SumF = SumF + Report_old(i,j,1);
SumA = SumA + Report_old(i,j,2);
if Report_old(i,j,2) > MaxAngle
MaxAngle = Report_old(i,j,2);
elseif Report_old(i,j,2) < MinAngle
MinAngle = Report_old(i,j,2);
end
end
MedoidF_old(1, i) = SumF/size(Report_old,2);
if (MaxAngle - MinAngle) > 350
MedoidA_old(1, i) = 0;
else
MedoidA_old(1, i) = SumA/size(Report_old,2);
end
end
MedoidF_new = zeros(1, size(Report_new,1));
MedoidA_new = zeros(1, size(Report_new,1));
for i=1:size(Report_new,1)
SumF = 0;
SumA = 0;
MinAngle = 361;
MaxAngle = -1;
for j=1:size(Report_new,2)
SumF = SumF + Report_new(i,j,1);
SumA = SumA + Report_new(i,j,2);
if Report_new(i,j,2) > MaxAngle
MaxAngle = Report_new(i,j,2);
elseif Report_new(i,j,2) < MinAngle
MinAngle = Report_new(i,j,2);
end
end
MedoidF_new(1, i) = SumF/size(Report_new,2);
if (MaxAngle - MinAngle) > 350
MedoidA_new(1, i) = 0;
else
MedoidA_new(1, i) = SumA/size(Report_new,2);
end
end
TempCluster = zeros(1, size(Report_new, 1));
CurrentCluster = ClusterCounter_old;
for i = 1: 1: size(Report_new,1)
for j = 1: 1: size(Report_old,1)
if (abs(MedoidF_old(1,j)-MedoidF_new(1,i))<FreqEpsilon &&...
(abs(MedoidA_old(1,j)-MedoidA_new(1,i))<DegreeEpsilon ||...
abs(360 + MedoidA_old(1,j)-MedoidA_new(1,i))<DegreeEpsilon ||...
abs(-360 + MedoidA_old(1,j)-MedoidA_new(1,i))<DegreeEpsilon)) %%if the new cluster is the rest of an old cluster use the old one's index for it
TempCluster(1,i) = Report_old(j,1,3);
end
end
%%this part is for seperating the clusters which where in the collision state in the past time
if (TempCluster(1,i)>0) %%if the new cluster is one of the old ones the index should be set
for j = 1:1:size(Report_new, 2)
Report_new(i,j,3) = TempCluster(1,i);
Report_new(i,j,4) = 1;% Alive
end
else %%first search if the new cluster is a part of a newly found cluster found before this one
for j = 1: 1: i-1
if (abs(MedoidF_new(1,j)-MedoidF_new(1,i))<FreqEpsilon &&...
(abs(MedoidA_new(1,j)-MedoidA_new(1,i))<DegreeEpsilon ||...
abs(360 + MedoidA_new(1,j)-MedoidA_new(1,i))<DegreeEpsilon ||...
abs(-360 + MedoidA_new(1,j)-MedoidA_new(1,i))<DegreeEpsilon)) %%if the new cluster is the rest of an old cluster use the old one's index for it
TempCluster(1,i) = Report_new(j,1,3);
end
end
end
if (TempCluster(1,i)>0) %%if the new cluster is one of the old ones the index should be set
for j = 1:1:size(Report_new, 2)
Report_new(i,j,3) = TempCluster(1,i);
Report_new(i,j,4) = 1;% Alive
end
else %%new cluster is just began so it needs a new index
CurrentCluster = CurrentCluster + 1;
ClusterCounter_new = CurrentCluster;
TempCluster(1,i) = CurrentCluster;
for j = 1:1:size(Report_new, 2)
Report_new(i,j,3) = TempCluster(1,i);
Report_new(i,j,4) = 1; % Alive
end
end
end
NewClusters = zeros(1, size (Report_new, 1));
for i = 1: size(Report_new, 1)
NewClusters (1,i) = Report_new(i,1,3);
end
OldClusters = zeros(1, size (Report_old, 1));
OldClustersLine = zeros(1, size (Report_old, 1));
for i = 1: size(Report_old, 1)
OldClusters (1,i) = Report_old(i,1,3);
OldClustersLine (1, i) = i;
end
NumberOfDead = 0;
%clear AddDead;
AddDead = zeros (16,size(Report_new, 2),4);
if (BlockCount>10)
for i = 1: size (OldClusters, 2)
IsDead = 1;
for j = 1: size (NewClusters, 2)
if OldClusters(1, i) == NewClusters(1,j)
IsDead = 0;
end
end
if (IsDead == 1)
NumberOfDead = NumberOfDead + 1;
%clear TempLine;
TempLine = zeros(1, size(Report_old,2), 4);
TempLine(1,:,1:3) = Report_old(OldClustersLine(1, i),:,1:3);
for k= 1: size(TempLine, 2)
TempLine(1,k,4) = 0; % Dead
end
TempSize = size(TempLine, 2);
Thresh = size(Report_new, 2);
if (TempSize >= Thresh)
AddDead (NumberOfDead, 1:Thresh, 1:4) = TempLine(1,1:Thresh, 1:4);
else
for l = 1: Thresh-TempSize
TempLine(1, TempSize+l, 1:4) = TempLine(1, TempSize, 1:4);
end
AddDead (NumberOfDead, 1:Thresh, 1:4) = TempLine(1,1:Thresh, 1:4);
end
end
end
xR = size (Report_new,1);
if (NumberOfDead == 0)
Report_Clustered = zeros (size(Report_new,1),size(Report_new,2),size(Report_new,3));
else
Report_Clustered = zeros (size(Report_new,1) + NumberOfDead,size(Report_new,2),size(Report_new,3));
end
Report_Clustered (1:size(Report_new,1), :, :) = Report_new(:,:,:);
for i = 1: NumberOfDead
Report_Clustered(xR + i, :) = AddDead(i, :);
end
end
and I'm using matlab 2012a
Tnx.
From what you've said in the comments, it appears that you simply need to call
clear functions
from the command line before recompiling the function to allow Matlab to overwrite the files. See this Matlab forum or the documentation for clear for more detail.
I am reading some MATLAB code to hopefully improve my knowledge. I do not understand the use of NaN below in the 3rd line.
As I understand it ones is a N-by-N matrix of ones so why would you want to multiple them by NaN? I also do not understand the line where ret_usdvec is used again: ret_usdvec(sline(1,1):sline(1,2), :)... = tdata.
int_cos = length(usedolchk);
int_obs = length(pricedatew);
ret_usdvec = num2cell(NaN * ones(int_cos * int_obs, 4));
sline = ones(1, 2);
int_count_max = 400000;
int_count = 0;
for t = 1:int_obs
if (int_count == 0)
QES_DB = SEdatabase(data_base, '', '');
end
sql_statement = ['select idchk, co_name, pdatew, ret from D_RAWRETS'];
cursor = exec(QES_DB, sql_statement);
cursor = fetch(cursor);
tdata = cursor.data;
sline(1, 2) = sline(1, 1) + length(tdata(:, 1)) - 1;
ret_usdvec(sline(1, 1):sline(1, 2), :)...
= tdata;
sline(1, 1) = sline(1, 2) + 1;
int_count = int_count + length(tdata(:, 1));
if (int_count >= int_count_max) || t == int_obs
close(QES_DB);clear QES_DB
int_count = 0;
end
end
It looks to me like they are just allocating the space for the data with NaN values, and then filling in the data with the true values within the loop. That way, if there is any missing data, it will have the value NaN.
Posting the entirety of my code in the hope somebody can help me debug this crap. Really hoping to reach a conclusion soon because I've been toying with this for way too long.
I have here a function extract which is passed a grid of integers which represent letters (i.e A=1,Z=26) from my wordsearch function. Given a direction and a target word which is represented by a row vector of ints, it should iterate through the grid to find where the first letter exists and move in all directions from here for length of the word and extract the word e.g. if we are looking for [14, 5, 9, 2] and 14 is first positioned at (4,4) we should end up at (4,8).
The word is then compared in the search function and if it matches the target word, a line is drawn from first letter to the last on an image of the actual wordsearch.
I know my if and for loops are off in some places but I'm finding it difficult to correct my code so that it works. Help! One thing in particular I'm having difficulty with is controlling flow so that if after checking all directions from a square containing the first letter, the next instance of that letter is evaluated. Where would it be best to do this?
Code has lots of errors as it is and could do with a couple of pointers telling me where it needs altering or cleaning up.
%//A function to find a word in a grid.
function test = extract(grid, direction, target)
%//switch through different cases that allow us to move to any adjacent cell to the current
switch upper(direction)
case 1
rowdir = -1;
coldir = 0;
case 2
rowdir = -1;
coldir = 1;
case 3
rowdir = 0;
coldir = 1;
case 4
rowdir = 1;
coldir = 1;
case 5
rowdir = 1;
coldir = 0;
case 6
rowdir = 1;
coldir = -1;
case 7
rowdir = 0;
coldir = -1;
case 8
rowdir = -1;
coldir = -1;
end
[i, j] = size(grid);
len = length(target);
[row,column] = find(target(1)==grid); %//find the letter of the word we are looking for in grid
%//row and column of last letter having moved in a particular direction
rowN = row + (len-1) * rowdir;
colN = column + (len-1) * coldir;
%//trying to say here to only move in a particular direction if we don't go out of bounds.
%//not sure I've succeeded
if (rowN > 1) | (rowN < i) | (colN > 1) | (colN < j)
testword = []; %empty array created
for index = 1:len
index_1 = index-1;
%//on loop get the letter in adjacent cell for direction we have moved
word = grid(row + (index_1 * rowdir), column + (index_1 * coldir));
testword{index} = word; %//letters are appended to create word for which we compare.
%//get co-ords of start letter. change to pixel co-ordinates so we can evaluate on image
wordstart = [(row*30)-15, (column*30)-15 ];
wordend = [((row + (len-1 * rowdir))*30)-15, ((column + (len-1 * coldir))*30)-15];
end
else
word = '';
end
x1 = wordstart(1);
x2 = wordend(1);
y1 = wordstart(2);
y2 = wordend(2);
test = [ word , [x1,x2] , [y1,y2]]; %//only way I could think of to get all of these as outputs
end
%//test is the image we want to evaluate on
%//words is the list of words
function trial1 = wordsearch(test, words)
imagesc(test);
colormap(gray);
hold on;
grid = %//grid is a 15x15 matrix
[row, column] = size(grid);
for iword = 1 : length(words)
target = char(words(iword)) - 'a' + 1;
for i = 1:row
for j = 1:column
for direction_num = 1:8 %//for each direction
direction = directions(direction_num, :);
testword = extract(grid,direction,target);
if testword(1)==target %//if word we have extracted equals the target word
%//draw_line function takes x co-ordinates and y co-ordinates and plots line.
draw_line(testword(2),testword(3),testword(4),testword(5));
end
end
end
end
end
hold off;
end
#Dan
My extract function now looks like:
[i, j] = size(grid);
len = length(target);
[row,column] = find(target(1)==grid);
for ii = 1:length(row)
start_row = row(ii);
start_column = column(ii);
rowN = start_row + len-1 * rowdir;
colN = start_column + len-1 * coldir;
if (rowN > 1) || (rowN < i) || (colN > 1) || (colN < j)
testword = [];
for index = 1:len
index_1 = index-1;
word = grid(start_row + (index_1 * rowdir), start_column + (index_1 * coldir));
testword{index} = word;
wordstart = [(start_row*30)-15, (start_column*30)-15 ];
wordend = [((start_row + (len-1 * rowdir))*30)-15, ((start_column + (len-1 * coldir))*30)-15];
end
else
end
end
What would I put as an else statement to check the word if previous in that particular direction takes you out of bounds?
for iword = 1 : length(words)
target = char(words(iword)) - 'a' + 1;
for i = 1:row
for j = 1:column
for direction_num = 1:8 %//for each direction
For every word, you are looping through every element in the grid (i.e. the i and j loops) but you don't actually ever use these i or j values. So those two loops do nothing! This is because you seems to do all of that inside your extract function. So drop those two loops, they are wasting an inordinate amount of time.
Inside your extract function, you have a line [row,column] = find.... This will find ALL the possible starting points. So you actually need to loop through either of those somewhere. So instead of your if (rowN > 1) | (rowN < i) | (colN > 1) | (colN < j) I would suggest something more like:
for ii = 1:length(row)
start_row = row(ii);
start_column = column(ii);
%// And now re-use your code, but swap out all your row for start_row and your column for start_column
.
.
.
end
That is the loop that will go through each of the possible starting letters.