find closest points between two point set

point_set_1 =
20 482
19 359
45 438
61 248
90 403
104 95
149 335
148 392
161 73
186 29
188 236
189 319
200 162
208 70
204 198
203 343
214 250
225 307
233 171
238 205
237 245
253 148
264 362
281 34
300 341
306 88
305 203
328 234
326 164
330 20
364 199
424 241
433 314
491 187
point_set_2 =
99 399
104 95
149 335
148 392
158 82
184 238
190 320
202 343
236 246
263 361
299 342
330 20
493 193
%compute Euclidean distances:
for idx=1: length(point_set_1)
distances = sqrt(sum(bsxfun(@minus, point_set_2, point_set_1(idx,:)).^2,2));
%find the smallest distance and use that as an index into B:
closestForPin2toPin1(idx,:)= point_set_2(find(distances==min(distances)),:);


foreign command atop frack or genfrac should be used instead(amsmath)

Using \atop is abusing its functionality; if you want to turn a screw, sometimes a knife can help, but a screwdriver is surely better.

In other words, use \substack that has been specifically defined for this task and can accommodate any number of lines. It’s also easier to use even for two lines.


\sum_{1\le i\le n\atop i\ne j}\quad
\sum_{\scriptstyle 1\le i\le n\atop\scriptstyle i\ne j}\quad
\sum_{\substack{1\le i\le n\\ i\ne j}}


The first is wrong; the second is complicated to write. With \substack you also avoid the warning

Package amsmath Warning: Foreign command \atop;
(amsmath)                \frac or \genfrac should be used instead
(amsmath)                 on input line 6.

“Compared with” vs “Compared to”

Use “compared with” when you are looking for differences.

E.g. CEO’s now earn 419 times the pay of blue-collar workers, compared with 42 times their pay in the 1980’s.

Use “compared to” when highlighting (or comparing) the similarities of one thing to another.

E.g. The human heart can be compared to a pump.

Change histogram to curve in Matlab but not “fit”

Instead of automatically plotting a histogram using hist, you can get it to output the values:

[x, c] = hist(radius1,400);

x is the data in each bin, c the centre of each bin, so this replicates a histogram and then overplots a line on it (which will just connect the top of each bar so it may not look as smooth as you hoped):

hold on

It is possible to use fit with an anonymous function as a custom model, but that may be overkill in this situation.

Matlab: applying norm function to rows of matrix


 norms = sqrt(sum(A.^2,1))


 norms = sqrt(sum(A.^2,2))?

depending on whether your coordinates are in rows or in columns.


If readability is a bigger consideration than performance you might also consider:

norms = cellfun(@norm,num2cell(A,2));

This pattern is also adaptable to other operations along one dimension you might want to perform where MATLAB doesn’t support it natively.


From version 2017b onwards, you can use vecnorm.



get IEEEtran to work with the subcaption package

The problem is that there does not seem to be a way to prevent subcaption from taking control of the main caption formatting away from IEEEtran like the caption=false option does under subfig.sty. IEEEtran has to format captions differently depending on its mode. An admittedly crude hack that might work is simply to restore IEEEtran’s definition of \@makecaption:




And then code figures like:

\subcaption{Case I}\label{fig_first_case}%
\subcaption{Case II}\label{fig_second_case}%
\caption{Simulation results for the network.}

This did seem to work for me in one simple test, but more extensive testing is required.