Find if an element is repeated exactly m times in a vector

Check if some elements of the vector are repeated exactly m times. To do so, we can use two very useful funcitons, namely ‘unique’ and ‘ismember’. The former creates a new array from the input array including only its unique elements. The later checks if an element is a member of an array or a matrix. Here is the code:

function y = mTimes(x,m)
y = [];
C = unique(x);
for kk=1:numel(C)
if sum(ismember(x,C(kk)))== m
y = [y C(kk)];
end
x(x==C(kk))=[];
end
end

Example

>> x = [1 2 5 2 2 7 8 3 3 1 3 8 8 8];
>> mTimes(x,3)
>>
>> [2 3]
Posted in MATLAB, programming, Linux, Ubuntu, Software | Tagged , , , , , , , , , , , , , , , , , , , , | Leave a comment

A Script for Toolbox Validation in MATLAB

Sometimes you want to check (through your MATLAB code), if the user of your code has a specific toolbox. If you want to do it through command-line in MATLAB, you should use the following command:

>> ver

Otherwise, use the following piece of code, to detect the existance of a specific toolbox:

requiredToolboxes = 'Image Processing Toolbox';
v=ver;
[installedToolboxes{1:length(v)}] = deal(v.Name);
tf = all(ismember(requiredToolboxes,installedToolboxes));

So if the output is 1 means that the toolbox exists otherwise no.

Posted in MATLAB, SIMULINK, programming, Linux, Ubuntu, Software | Tagged , , , , , , , , , , , , , , , , , , , , , , | Leave a comment

People we should get to know – Part 1: John Bardeen

John Bardeen was a physicist and electrical engineer who have won the Nobel Prize twice. He is the only person who managed to won the Nobel prize twice in physics. The first time, he won the prize for the invention of the transistor that revolutionized the electronic industry and ignited the information age (1956). This invention made possible the development of almost every modern electronic device like telephones, computers, etc.

He won the Nobel prize again in physics for developing the theory of conventional superconductivity (1972). This theory is used in nuclear magnetic resonance spectroscopy or its medical sub-tool magnetic resonance imaging known as MRI.

Posted in Thoughts | Tagged , , , , , , , , , , , , , , , | Leave a comment

Estimating the value of Pi using Python (For educational Purposes)

You can estimate the value of `Pi` up to specific precision with the following method:

1- draw a square and draw the corresponding circle inside it (a quarter of the circle inscribed inside the square). So the edge length of the square and the radius of the circle would be equal (r).

Pi_30K

 

 

 

 

 

 

 

 

 

 

 

2- sample some points in the boundaries of the square uniformly.

3- Count the number of points inside the circle (obviously all the points are inside the square)

4- The area of the square is r^2 and the area of the circle is pi*r^2 / 4. By considering the fact that finally the sampled points can fill the whole area of the square we can assume that the number of points inside the square describe the area of the square and the number of points inside the circle describe the area of the circle. So if we divide the number of points that are inside the circle by the number of total points the result is:
[(pi*r^2)/4] / [r^2]

Now by multiplying the obtained value we will have Pi.

Let’s show this in a simple code:

from __future__ import division
import numpy as np

a = 1
n = 1000000
s1 = np.random.uniform(0,a,n)
s2 = np.random.uniform(0,a,n)

ii=0
jj=0

for item in range(n):
if ((s1[item])**2 + (s2[item])**2) < 1:
ii = ii + 1

# A(circle) = pi/4
# A(square) = 1
print float(ii*4/(n))

This method is called Monte Carlo and you can find more information starting from wikipedia. (The gif image is also borrowed from Wikipedia).

Posted in programming, Linux, Ubuntu, Python, Software | Tagged , , , , , , , , , , , , , , , , , , , , , | Leave a comment

Step-by-step Tutorial on Face Detection with MATLAB – Part 1

This is a step-by-step, easy to use, beginner level, and detailed tutorial for face detection with MATLAB. Let’s start!!

List available devices

In order to list the available webcams that can be detected and used by MATLAB do as follows:

webcamlist

The result should be something similar to this:

ans =
'FJ Camera'

 

Webcam is not detected

If MATLAB cannot detect your webcam, you need to install the package for USB cameras provided by mathworks.
Enter the following command in MATLAB command-line and press enter:

supportPackageInstaller

This will bring up a window that you need to select an action, Install from Internet is a good option. Then you have to scroll down and find USB camera and select it. After that MATLAB will ask you to log in with your mathworks account. If you do not have it, you can create one. Finally, the process will begin and you have to wait a little bit.

In order to see that everything is fine, check to see if MATLAB can find the camera:

webcamlist

The result should be something similar to this:

ans =
'FJ Camera'

 

Preview webcam

Firstly, get a list of existing (detectable) webcams. And use the one that you want to make a webcam object. In the following example I have chosen the first item in the list.

C=webcamlist;
cam=webcam(C{1});

When the object is created, you can check its properties by just typing the name of the object:

>> cam
cam =
Name: 'FJ Camera'
Resolution: '640x480'
AvailableResolutions: {'640x480'  '352x288'  '320x240'  '176x144'  '160x120'  '1280x1024'}
Gain: 0
Contrast: 32
BacklightCompensation: 1
Saturation: 64
Brightness: 0
Gamma: 100
Sharpness: 0
WhiteBalance: 4600
Hue: 0

To get a preview, you can do as follows:

preview(cam)

To close the preview windows:

closePreview(cam)

 


 

Capture a single Image

To capture a single image from the webcam while the preview window is open, you can use the following commands:

img = snapshot(cam);

The result would be captured as a matrix in MATLAB called img. To show the image in a new window you can use:

imshow(img);

if you have the image processing toolbox, otherwise use:

image(img);

remember if you close a preview using closePreview command, you should also clear the object from the memory:

clear('cam');

Simple face detection

In order to do a simple face detection, you need to take a snapshot and check to see if there is a face in there.first create a face detection object. (you have to have the computer vision toolbox).

faceDetector = vision.CascadeObjectDetector;

and then easily pass the taken snapshot to the face detector. If it finds a face in the input image, the return would be 4 coordinates of the corners a box around the detected face.

bboxes = step(faceDetector, img);

 

Source Code – A simple face detection by MATLAB

Because most of webcam cameras are low quality and noisy, you can use the following loop, in order to avoid manual repetition.

C = webcamlist;
cam=webcam(C{1});
preview(cam);
NotYet = false;
faceDetector = vision.CascadeObjectDetector;
while ~NotYet
pause(2);
I = snapshot(cam);
disp('took a snapshot. checking to find a face ....')
bboxes = step(faceDetector, I);
if ~isempty(bboxes)
NotYet = true;
disp('face found!');
break;
end
disp('no face detected :(, repeating...');
end
closePreview(cam);
clear('cam');
IFaces = insertObjectAnnotation(I, 'rectangle', bboxes, 'Face');
figure, imshow(IFaces), title('Detected faces');

 

 

Posted in Robotics, Machine Learning, MATLAB, programming, Linux, Ubuntu, OpenCV | Tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , | Leave a comment

Efficient coding in MATLAB – part 1

Question: Given a matrix, test to see if any of its elements are greater than 3.

A = rand(3,7,5) * 5;

Non-efficient code:

b = ~isempty(find(A>3));

More efficient code:

b = ~isempty(find(A(:)>3));

Most efficient code:

b = any(A(:)>3);

check the documents for any function.
short description: Determine if any array elements are nonzero


 

Question: Pick odd elements from the matrix below:

A = [1 -3 2;5 4 7;-8 1 3];

Non-efficient code:

k=1;
B=[];
for ii=1:size(A,1)
for jj=1:size(A,2)
if mod(A(ii,jj),2)~=0
B(k)=A(ii,jj);
k=k+1;
end
end
end

More efficient code:

B=mod(A,2);
[r,c,v] = find(B==1);

Most efficient code:

B=A(logical(mod(A,2)));

check the documentation for logical function.
short description: Convert numeric values to logicals

 

 

Posted in MATLAB, programming, Linux, Ubuntu, Optimization, Software | Tagged , , , , , , , , , , , , , , , , , , , , , , , , , | Leave a comment

What is a DOI?

A DOI is a unique permanent number used to identify digital content which stands for Digital Object Identifier. For instance, a DOI can be generated for a journal paper you have published. DOIs provide a reliable link to your work online, making it easy to be cited by others. DOIs can be generated for both published and unpublished works. Publishers generate DOIs for papers they publish. A DOI also can be generated by other websites as well, for instance ResearchGate. If you add a contribution to your account in ResearchGate, the website generates a DOI for the file you have uploaded. When you have a DOI for an existing publication you can just search the DOI number and it takes you the corresponding page.

 

 

Posted in Linux, Ubuntu, Software, Thoughts | Tagged , , , , , , , , , , | Leave a comment