Does anyone know how Matlab’s Z=dist(W,P) works? The Matlab documentation does not specify an algorithm that uses the weight matrix, W.
I am using Octave and trying to mimic the behavior. So far, this stack overflow post helped me to prove that Octave’s Z=squareform(pdist(P')) is equivalent to Matlab’s Z=dist(P). I can also designate the elements of Z using a couple of for loops:
function z=dist(w,p)
for i=1:size(p,2)
for j=1:size(p,2)
u=p(:,i);
v=p(:,j);
z(i,j)=sum((u-v).^2).^0.5;
end
end
end
However, I am unable to find any online documentation on the weight matrix, W. I cannot simply multiply the output of squareform(pdist(P')) by W because the dimensions do not match. I have attempted to premultiply W, e.g. sqrt(W*(P(:,1)-P(:,2)).^2), like this stack overflow post but the output magnitudes are wrong.
Here is an example using Matlab:
P=
9 7 10
10 1 10
2 3 2
10 6 10
W=
10 9 5 8
5 2 10 10
>> D = dist(P)
0 10.0995 1.0000
10.0995 0 10.3441
1.0000 10.3441 0
>> D = dist(W,P)
3.8730 9.0000 3.7417
12.0000 8.3666 12.3693
Thanks in advance for any help you can provide!
It appears the Neural Network Toolbox is implemented in Matlab itself, so you can just look at those source files and figure it out.
If you open dist by entering edit dist in the command window, you see that it calls dist.apply or dist.distance to do the actual work, and the latter again dist.apply. Therefore, my guess is that what you are looking for can be found on line 9 of
toolbox/nnet/nnet/nndistance/+dist/apply.m
Type edit dist.apply in the command window to see it.
Related
I use MATLAB version R2015a.
I get a series answer from solving the optimal problem several times, and I want to get their sum and average them. However, some of them are NaN. How do I write code to ignore those NaN and sum the others which are not NaN?
Option 1: toolbox free solution using sum and isnan from base MATLAB.
A = [1 2 3 4 5 6 7 8 9 NaN];
sum(A(~isnan(A))) % No toolbox required
Option 2: nansum (see this answer from OP)
Note: nansum requires the Statistics toolbox.
nansum(A) % Requires Statistics toolbox
Code tested using MATLAB R2018b.
Update from comments
Great suggestion from #Cris Luengo for those with more recent versions. Requires no toolbox.
sum(A,'omitnan') % No toolbox required
Another suggestion from #Ben Voigt for some applications. Also requires no toolbox.
sum(A(isfinite(A))) % No toolbox required
You can use inbuilt functions as suggested in the above answer. If you want to know the logic and use a loop..you can follow as shown below:
A = [NaN 1 2 NaN 3 4 7 -1 NaN] ;
count = 0 ;
thesum = 0 ;
for i = 1:length(A)
if ~isnan(A(i))
count = count+1 ;
thesum = thesum+A(i) ;
end
end
You can use the omitnan argument
A = [1 2 3 4 5 6 7 8 9 NaN];
s = sum( A, 'omitnan' )
Note, this is literally the same code as used by the nansum function from the Statistics toolbox, which was introduced before R2006a, so I would think compatibility is pretty good.
If I have [1 2 3] and I need to respectively multiply it by [2 3 4] to make the array [1*2 2*3 3*4], how do I do that?
I can't seem to do [1 2 3]*[2 3 4] = [1*2 2*3 3*4]
My actual issue is:
At=sum(abs([1 2 3].*exp(i.*[4 5 6])))
Tried rewriting it with a bsxfun inside the sum, but I get an error about not enough elements. It works with [1x1] and [1x1], but I tried with [1x6000] and [1x6000] and AT is equal one value. Not sure if the sum there is a good idea.
How would I go about performing the above operation so that the 1 and 4 are affected together, the 2 and 5 are affected together, and the 3 and 6 are affected together such that 'At' gives a 3x1 or 1x3 array?
Thank you so much!
EDIT: Alright so it seems that the exponential function "works".
What doesn't work is that it either adds everything together or creates twice as many solutions as I need.
If I feed it this kind of info:
A1=[1 2]
A2=[1 2]
p=[0.1 0.2]
p=[0.1 0.2]
>> [At] = somme_signaux([A1 A2],[p1 p2]);
>> At
At = 1.0000 2.0000 1.0000 2.0000
I'm going to get 4 answers without the sum. I need 2 answers (the first and second answer added together (1+2), and the third and fourth answer added together(1+2)) into a 2 by 1 matrix.
I don't really get your issue? .* can be used for element-wise multiplication, i.e.
>> [1 2 3].*[2 3 4]
ans =
2 6 12
Also, the code with the complex exponential works for me.
So I am writing a function that plots matrix data from n different cells. If n is 10, it should display 10 equally spaced plots on a single figure. If n is 7, it should try to space them out as equally as possible (so 3x2 or 2x3 plots with a plot by itself).
I am able to get these graphs drawn using subplot() and plot() but I'm having a hard time finding out how to initialise the dimensions for the subplot.
The number of subplots will be changing after each run so I can't initialise it to specific dimensions.
Can anyone point me in the right direction?
I am afraid problems like this tend to be messy. This normally problems like this need to be solved for different cases.
if (mod(n,2) && n<8)
% Do something
elseif (!mod(n,2) && n < 11)
% Do something else
elseif ...
....
end
The conditions are choosen a bit arbitarily since the specifications in the OP seemed a bit arbitary too. You probably understand the point and can set your own conditions.
There are two reasons why I recommend this approach.
1) This makes the code simpler to write. You do not have to come up with some complicated solution which may break in after some time.
2) By adding cases you can protect yourself against a rampant number of plots. In case the number of plots gets too large you do typically not want to have all plots in the same figure. It is also possible to wrap this into a function and apply this to X plots at a time in a loop. Typically you would want each iteration to be a separate figure.
It is not very easy to elaborate more on this since you have not yet specified how many cases you expect or what will happen to the last plot in case of odd numbers. Still this may give a good hint.
Good luck!
Another simple solution would be using round and ceil on the square root:
for n=1:20
[n, round(sqrt(n))*ceil(sqrt(n)), round(sqrt(n)), ceil(sqrt(n))]
end
output:
%(n, total_plots, x, y)
1 1 1 1
2 2 1 2
3 4 2 2
4 4 2 2
5 6 2 3
6 6 2 3
7 9 3 3
8 9 3 3
9 9 3 3
10 12 3 4
Usage example:
n = 7
subplot(round(sqrt(n)), ceil(sqrt(n)), plot_nr_x) % switch first 2 params to have either a slightly longer or slightly wider subplot
I ran into a very similar problem today and I was having a lot of trouble to define the size of the subplot that would fit everything. My reasoning is mostly a hack but it can help. If you have to represent at most n figures, you can thing as a square grid of sqrt(n) * sqrt(n). To make things better we add a safety row, so the final matrix would be (sqrt(n) + 1) * sqrt(n). I hope this helps solving your problem.
In my code have 2 nested loops:
within a loop that opens a figure for each kk element and is meant to plot a particular graph from the x position within the array.
for kk=1:length(some_file_list)
% Load data
% do some math
% get data as a cell array with things we care about in data(3,)
array_size = size(data(3,:),2);
for x=1:size(data(3,:),2);
% do more math and get things ready to plot matrix_A scaled by range_A
figure(kk); % open figure
grid_rows = round((sqrt(array_size)+1));
grid_cols = round(sqrt(array_size));
% plot
subplot(grid_rows, grid_cols, x);
imagesc(matrix_A,range_A); %plot in position
colormap(gray);
end
end
This question already has answers here:
Vector norm of an array of vectors in MATLAB
(4 answers)
Closed 7 years ago.
I know quite well how to use R but i'm new to Matlab
suppose I have the simple matrix
y =
1 2
3 4
5 6
i want to compute row by row the (euclidean) norm of the vector rows. and return it in a column
vector.
>> norm(y(1,:))
ans = 2.2361
but when i put the following command, i get an error... whats wrong ?
>> rowfun(norm,y)
Error using norm
Not enough input arguments.
you are looking for the norm of every row, you can do this by using arrayfun instead of rowfun as rowfun is more used in table structures. The 1 liner is:
result = arrayfun(#(idx) norm(y(idx,:)), 1:size(y,1));
result =
2.2361 5.0000 7.8102
This is a question about Matlab/Octave.
I am seeing some results of the medfilt1(1D Median filter command in matlab) computation by which I am confused.
EDIT:Sorry forgot to mention:I am using Octave for Windows 3.2.4. This is where i see this behavior.
Please see the questions below, and point if I am missing something.
1] I have a 1D data array b=[ 3 5 -8 6 0];
out=medfilt1(b,3);
I expected the output to be [3 3 5 0 0] but it is showing the output as [4 3 5 0 3]
How come? What is wrong here?
FYI-Help says it pads the data at boundaries by 0(zero).
2] How does medfilt2(2D median filter command in matlab) work.
Help says "Each output pixel contains the median value in the m-by-n neighborhood around the corresponding pixel in the input image".
For m=3,n=3, So does it calculate a 3x3 matrix MAT for each of input pixels placed at its center and do median(median(MAT)) to compute its median value in the m-by-n neighbourhood?
Any pointers will help.
thank you. -AD
I was not able to replicate your error with Matlab 7.11.0, but from the information in your question it seems like your version of medfilt1 does not differentiate between an odd or even n.
When finding the median in a vector of even length, one usually take the mean of the two median values,
median([1 3 4 5]) = (3+4)/2 = 3.5
This seems to be what happens in your case. Instead of treating n as odd, and setting the value to be 3, n is treated as even and your first out value is calculated to be
median([0 3 5]) = (3+5)/2 = 4
and so on.. EDIT: This only seems to happen in the endpoints, which suggest that the padding with zeros is not properly working in your Octave code.
For your second question, you are almost right, it calculates a 3x3 matrix in each center, but it does not do median(median(MAT)), but median(MAT(:)). There is a difference!
A = [1 2 3
14 5 33
11 7 13];
median(median(A)) = 11
median(A(:)) = 7