Octave complex element-wise operations - matlab

Is there a way to write 'complex' element-wise operations in one line, or do we have to separate them into multiple lines?
For example, let us have this mathematical function: 1/(1+e^-x)
Which I want to calculate for each element on x (x may be a scalar, vector or a matrix).
This is a working code I have written:
function r = sigmoid(x)
r = zeros(size(x));
r = e.^(-x);
r = 1.+r;
r = 1./r;
end
My question is - can we simplify it to one line?

yeah you can do it by below function
function r=sigmoid(x)
r=1./(1+exp(-x))
end
this first consider that the exp function calculate exponential values wise element and added by one and finally result divided 1 over matrix element wise and you can get what you want.

Related

Vectorize function evaluation in MATLAB

I have the following function,
function Vectorize()
a = randn(1,5)
b = randn(1,5)
c = zeros(1,5)
for i=1:5
c(i) = (a(i) - b(i))/(1+a(i)/2+b(i)/3)
end
I want to vectorize the above function evaluation and replace the for loop.
I could do c = a -b, that finds the difference between two row vectors.I am not sure how to handle the division a/2 and b/2.
Could someone help?
You need the element wise division operation ./
c = (a - b)./(1+a/2+b/3)
If you divide a vector by a scalar, this is not required, but where you divide an array by an array you will have to use ./ in your case. See here for the other element wise operators.

Can someone help vectorise this matlab loop?

i am trying to learn how to vectorise matlab loops, so im just doing a few small examples.
here is the standard loop i am trying to vectorise:
function output = moving_avg(input, N)
output = [];
for n = N:length(input) % iterate over y vector
summation = 0;
for ii = n-(N-1):n % iterate over x vector N times
summation += input(ii);
endfor
output(n) = summation/N;
endfor
endfunction
i have been able to vectorise one loop, but cant work out what to do with the second loop. here is where i have got to so far:
function output = moving_avg(input, N)
output = [];
for n = N:length(input) % iterate over y vector
output(n) = mean(input(n-(N-1):n));
endfor
endfunction
can someone help me simplify it further?
EDIT:
the input is just a one dimensional vector and probably maximum 100 data points. N is a single integer, less than the size of the input (typically probably around 5)
i don't actually intend to use it for any particular application, it was just a simple nested loop that i thought would be good to use to learn about vectorisation..
Seems like you are performing convolution operation there. So, just use conv -
output = zeros(size(input1))
output(N:end) = conv(input1,ones(1,N),'valid')./N
Please note that I have replaced the variable name input with input1, as input is already used as the name of a built-in function in MATLAB, so it's a good practice to avoid such conflicts.
Generic case: For a general case scenario, you can look into bsxfun to create such groups and then choose your operation that you intend to perform at the final stage. Here's how such a code would look like for sliding/moving average operation -
%// Create groups of indices for each sliding interval of length N
idx = bsxfun(#plus,[1:N]',[0:numel(input1)-N]) %//'
%// Index into input1 with those indices to get grouped elements from it along columns
input1_indexed = input1(idx)
%// Finally, choose the operation you intend to perform and apply along the
%// columns. In this case, you are doing average, so use mean(...,1).
output = mean(input1_indexed,1)
%// Also pre-append with zeros if intended to match up with the expected output
Matlab as a language does this type of operation poorly - you will always require an outside O(N) loop/operation involving at minimum O(K) copies which will not be worth it in performance to vectorize further because matlab is a heavy weight language. Instead, consider using the
filter function where these things are typically implemented in C which makes that type of operation nearly free.
For a sliding average, you can use cumsum to minimize the number of operations:
x = randi(10,1,10); %// example input
N = 3; %// window length
y = cumsum(x); %// compute cumulative sum of x
z = zeros(size(x)); %// initiallize result to zeros
z(N:end) = (y(N:end)-[0 y(1:end-N)])/N; %// compute order N difference of cumulative sum

MATLAB: bsxfun unclear. Want to accelerate minimum distance between segments

Using MATLAB,
Imagine a Nx6 array of numbers which represent N segments with 3+3=6 initial and end point coordinates.
Assume I have a function Calc_Dist( Segment_1, Segment_2 ) that takes as input two 1x6 arrays, and that after some operations returns a scalar, namely the minimal euclidean distance between these two segments.
I want to calculate the pairwise minimal distance between all N segments of my list, but would like to avoid a double loop to do so.
I cannot wrap my head around the documentation of the bsxfun function of MATLAB, so I cannot make this work. For the sake of a minimal example (the distance calculation is obviously not correct):
function scalar = calc_dist( segment_1, segment_2 )
scalar = sum( segment_1 + segment_2 )
end
and the main
Segments = rand( 1500, 6 )
Pairwise_Distance_Matrix = bsxfun( #calc_dist, segments, segments' )
Is there any way to do this, or am I forced to use double loops ?
Thank you for any suggestion
I think you need pdist rather than bsxfun. pdist can be used in two different ways, the second of which is applicable to your problem:
With built-in distance functions, supplied as strings, such as 'euclidean', 'hamming' etc.
With a custom distance function, a handle to which you supply.
In the second case, the distance function
must be of the form
function D2 = distfun(XI, XJ),
taking as arguments a 1-by-N vector XI containing a single row of X, an
M2-by-N matrix XJ containing multiple rows of X, and returning an
M2-by-1 vector of distances D2, whose Jth element is the distance
between the observations XI and XJ(J,:).
Although the documentation doesn't tell, it's very likely that the second way is not as efficient as the first (a double loop might even be faster, who knows), but you can use it. You would need to define your function so that it fulfills the stated condition. With your example function it's easy: for this part you'd use bsxfun:
function scalar = calc_dist( segment_1, segment_2 )
scalar = sum(bsxfun(#plus, segment_1, segment_2), 2);
end
Note also that
pdist works with rows (not columns), which is what you need.
pdist reduces operations by exploiting the properties that any distance function must have. Namely, the distance of an element to itself is known to be zero; and the distance for each pair can be computed just once thanks to symmetry. If you want to arrange the output in the form of a matrix, use squareform.
So, after your actual distance function has been modified appropriately (which may be the hard part), use:
distances = squareform(pdist(segments, #calc_dist));
For example:
N = 4;
segments = rand(N,6);
distances = squareform(pdist(segments, #calc_dist));
produces
distances =
0 6.1492 7.0886 5.5016
6.1492 0 6.8559 5.2688
7.0886 6.8559 0 6.2082
5.5016 5.2688 6.2082 0
Unfortunately I don't see any "smarter" (i.e. read faster) solution than the double loop. For speed consideration I'd organize the points as a 6×N array, not the other way, because column access is way faster than row access in MATLAB.
So:
N = 150000;
Segments = rand(6, N);
Pairwise_Distance_Matrix = Inf(N, N);
for i = 1:(N-1)
for j = (i+1):N
Pairwise_Distance_Matrix(i,j) = calc_dist(Segments(:,i), Segments(:,j));
end;
end;
Minimum_Pairwise_Distance = min(min(Pairwise_Distance_Matrix));
Contrary to common wisdom, explicit loops are faster now in MATLAB compared to the likes of arrayfun, cellfun or structfun; bsxfun beats everything else in terms of speed, but it doesn't apply to your case.

Find entrance of matrix for each adjacent pair of numbers in vector and multiply

I have a (transition) function defined by a matrix say P=[0.75,0.25;0.25,0.75] and I have a vector say X=[1,2,1] then i would like to find P(1,2)*P(2,1). How is the easiest way to generalise this? I tried creating a function handle for P(i,j) and then X_temp=[X(1:end-1);X(2:end)], using the function of each column and finally using the product function, but it seems a lot more comprehensive than it has to be.
The X i want to use is 1000 dimensional and P is 3x3 and I would have to repeat it a lot of times so speed I think will matter.
You can use sub2ind to get your relevant P values:
Ps = P(sub2ind(size(P), X(1:end-1), X(2:end)))
Now just multiply them all together:
prod(Ps)
EDIT:
For function handles you had the right idea, just make sure that you function itself handles vectors. For example lets say your function f(i,j) = i + j, I'm going to assume it's actually f(x) = x(1) + x(2) but I want it to handle many xs at once sof(x) = x(:,1) + x(:,2):
f = #(x)(x(:,1) + x(:,2))
f([X(1:end-1)', X(2:end)'])
OR
f = #(ii, jj)(ii + jj)
f(X(1:end-1)', X(2:end)') %//You don't actually need the transposes here anymore
just note that you need to use element wise operators such as .*, ./ and .^ etc instead of *, /,^...

MATLAB: Create a block diagonal matrix with same repeating block

I have a matrix K of dimensions n x n. I want to create a new block diagonal matrix M of dimensions N x N, such that it contains d blocks of matrix K as its diagonal.
I would have directly used M = blkdiag(K,K,K) etc. had d been smaller. Unfortunately, d is very large and I don't want to manually write the formula with d exactly same arguments for the blkdiag() function.
Is there any shorter, smarter way to do this?
you can use kron for that.
M = kron(X,Y)
returns the Kronecker tensor product of X and Y. The result is a large array formed by taking all possible products between the elements of X and those of Y. If X is m-by-n and Y is p-by-q, then kron(X,Y) is m*p-by-n*q. So in your case something like this will do:
M = kron(eye(L),K)
with L the # of blocks.
tmp = repmat({K},d,1);
M = blkdiag(tmp{:});
You should never use eval, or go into for loops unnecessarily.
Kron is a very elegant way.
Just wanted to share this as it also works.
The following should work:
d=5; K=eye(3); T = cell(1,d);
for j=1:d
T{j} =K;
end
M = blkdiag(T{:})
s = 'A,';
s = repmat(s,[1,n2]);
s = ['B=blkdiag(', s(1:end-1),');'];
eval(s);
It can be faster than using kron-eye.
A "for" loop may might help. Like:
M = k;
for i=1:N/n - 1
M=blkdiag(M,k);
end