How to vectorize for loops in Matlab? [closed] - matlab

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a matrix where each row is a vector of data points and I want to predict labels for each row using a classifier. What I'm doing now is reading each row of the matrix one by one in a for loop and running the classifier on each entry. There's room for parallelization here since each row's outcome is independent of the other rows. Does MATLAB have any function to speed this up?

Assuming you wrote the classifier yourself this is possible. Instead of passing one row to the classifier you would pass the complete matrix. Within the classifier you would have to change the way the prediction is made. Assuming you use a mathematical formula to calculate the outcome you would have to alter it using element-wise operators. An example of using element-wise operators instead of a for-loop:
Example data
a = [1,2;
3,4;
5,6]
Using a for-loop
p1= zeros(size(a,1),1);
for i=1:size(a,1)
p1(i) = a(i,1)*2 + a(i,2)^2;
end
Using vectorization
p2 = a(:,1).*2 + a(:,2).^2
Element-wise operators:
http://nl.mathworks.com/help/matlab/ref/times.html?searchHighlight=element-wise
http://nl.mathworks.com/help/matlab/ref/power.html?searchHighlight=element-wise
It is highly recommended to use vectorization instead of for-loops since it's much more efficient.

Related

Regression Matlab [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 days ago.
Improve this question
Identification of brain areas after language stimuli
Hi guys, I'm trying to solve a problem with my code in matlab. My goal is to identificate brain areas associated to language stimuli. After I performed some tasks, i arrived to plot the convolution between the BOLD signal and square signal (stimuli). Now, my goal is to perform a regression using regress between the convolution result (1x481 matrix) and a matrix containing the number of voxels of my brain images (92614x39) where the rows are the voxels and 39 are the MRI scans. So, the two matrices have different size and I have no idea how to perform a regression. Anyone could help me?

Primal form of Soft margin SVM implementation in matlab [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am implementing primal form of soft margin svm. After calculating the weight and bias parameters, I need to test it on test data. How can I achieve that? I need to calculate the accuracy of the test data classification. Thank you.
Thank you.
The classification rule of your SVM is (no matter if you trained it in soft or hard margin rule):
cl(x) = sign(<w, x> - b) = sign( SUM_i w_i x_i - b )
where w_i are your coefficients and b is a bias.
Just take your test set, pass it through this rule and calculate the fraction of correct predictions (accuracy).

Generate a random variable with an exponential distribution [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In Matlab using the rand routine, how should I write the code to generate 500 samples from an exponential distribution, whose pdf is:
(1/mu)*exp(-x/mu); x>=0
Assuming you really have to do it using the rand function: exploit the property that the minus logarithm of a normalized uniform RV is a normalized exponential RV:
samples = -mu*log(rand(1,500));
Use random function.
For example to create a 4*6 matrix with mu=1.3 with an exponential distribution use:
random('Exponential',1.3,4,6)
or
random('exp',1.3,4,6)
If you have the Statistic toolbox you can simply use exprnd much like you use rand:
r = exprnd(mu);
where the size of r will be the size of the mean, mu, or
r = exprnd(mu,m,n);
where mu is a scalar mean, and m and n are the size of your desired output. If you type edit exprnd, you'll see that the code is virtually identical to that kindly provided by #LuisMendo. You might find the other functions related to the exponential distribution helpful to, such as exppdf and expcdf. These are simple as well and implement basic equation that you can find in your textbook or on Wikipedia.

matlab indices not in range? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to write the optimal quantization for IP.
I'm new to matlab and in this code, i'm trying to go over every pixel in every interval of Z, multiply it with it's histogram and sum it , so I can calculate the optimal Q.
problem : Attempted to access hist(257);index out of bounds because numel(hist)=256.
for i=1:K,
for j=(Z(i)):Z(i+1),
sum1=(j)*hist(j+1)+sum1;
count=count+hist(j+1);
end
end
The error is telling you that you cannot access hist(257) because the array hist only has 256 elements in it. Note that hist is also a built in function name so you really ought to consider giving your variable a different name.
How to solve:
Think carefully about your code, and what you are trying to achieve. What are Z. hist and K? What is the largest value that j can reach (=Z(i+1))? That is the value with which you are indexing hist, and apparently hist is not that big. What then is the shape of each variable?

Image processing: Minimizing laplacian norm with boundary conditions [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I'm trying to minimize this function (by A):
argmin_A (||L(A)||^2 + a||A-B||^2*)
where:
A is a MxN image L is the Laplacian Operator
||.|| is the usual norm (Frobrenius)
a is a weight parameter
B is a matrix of size (M+2*k)xN
where k is an integer parameter.
(*) indicates that we just consider the pixels in the boundary (we want to preserve in A the pixels in the boundary of B).
Maybe the problem has a trivial solution, but I'm absolutely blocked.
If you need more details, it's (4) equation in this paper.
I will be very grateful for any help provided.
Without looking carefully at that paper, gridfit does essentially that, although it does not employ the boundary conditions you ask for. You could certainly write what you want however, or you could simply apply a weight to the boundary points. This is not a difficult problem, but in order to solve it efficiently, you would want to employ the sparse matrix capabilities of MATLAB.