Saturday, April 18, 2020

MATLAB Code Errors Debugging

1.Error in below code "Array indices must be positive integers or logical values" ?



deposits = [1000,1200,900,1000,500,800];
rates = [0.08,0.02,0.09,-0.01,0.04,0.05];
balance = deposits(1);
intrest = 0;
for i=i:7 – Error ,It should be I = 1:7
intrest = balance * rates;
balance = balance  + deposits(i) + intrest;
end




2.Incorrect dimensions for matrix multiplication. Check that the number of columns
in the first matrix matches the number of rows in the second matrix.
To perform element wise multiplication, use '.*' ?



deposits = [1000,1200,900,1000,500,800];
rates = [0.08,0.02,0.09,-0.01,0.04,0.05];
balance = deposits(1);
interest = 0;
for i=1:7
interest = balance * rates; – Error, It should be “.*” instead of “*”
balance = balance  + deposits(i) + interest;
end



3.Index exceeds the number of array elements (6).?



deposits = [1000,1200,900,1000,500,800];
rates = [0.08,0.02,0.09,-0.01,0.04,0.05];
balance = deposits(1);
interest = 0;
for i=1:7Error, It should be “1:6” instead of “1:7” bec index of vector is “1*6”
interest = balance * rates;
balance = balance  + deposits(i) + interest;
end



4.Open code files and create breakpoint to debug more on the specific line ?
Open the code file in the editor and provide the breakpoint to debug more

                                               

MATLAB Basics Part 2

1. What is the use of 'hold on' while plotting ?
'hold on' command is used for plotting two or more graphs on a single graph of difference function.

2.How to draw Multiple Plots ? How to create legend for multiple plots?
Script :
>>month = 1:1:12;
>>maxTemp= linspace(15,50,12);
>>avgTemp= linspace(10,30,12);
>>p1 = bar(month,maxTemp,'yellow');
>>hold on;
>>p2 = plot(month,avgTemp,'red');
>>xlabel('Months');
>>hold off;
>>ylabel('Temprature');
>>title('Town Temparature')
>>legend([p1,p2],{'maxTemp','avgTemp'})



legend syntax for multiple plots :
>>legend([p1,p2],{'maxTemp','avgTemp'})




3.How to access multiple elements of matrix at a time ?
Script :
data = [18,32,26,28,46;25,42,35,30,52;
        43,44,37,52,54;49,38,59,54,55;
        55,48,61,69,62;48,34,56,42,56];
rows = [1,5];
cols = 2:4;
subdata = data(rows,cols);%{Fetches the data only part of the given selection}%





4.Mean of Matrix ? Example ?
Mean = (sum of all elements )/(total elements) ;

Mean Of Matrix :
1.If A is a vector, then mean(A) returns the mean of the elements.
2.If A is a matrix, then mean(A) returns a row vector containing the mean of each column.
3.If M = mean(A,'all') computes the mean over all elements of A
4.If M = mean(A,dim) returns the mean along dimension dim. For example, if A is a matrix, then mean(A,2) is a column vector containing the mean of each row.

Script :

A = [0 1 1;
     2 3 2;
     1 3 2;
     4 2 2];
M = mean(A)
M = mean(A,2)
M = mean(A,'all')


5.Built in functions for Matrix Creation ,Name few?


Function Name
Use
eye
Identity Matrix
zeros
Matrix of all 0's
random
Normally disturbed random numbers
diag
Diagonal Matrix
Ones
Matrix of all 1's
rand
Uniformly distributed random numbers
randi
Uniformly distributed random integers

Script :
>>I= eye(5);
>>Z = zeros(5);
>>ZM = zeros(5,3);
>>Rn = randn(3,3);
>>D = diag(3,3);
>>O = ones(4,4);
>>R = rand(2,2);
>>R1 = randi(2,2);



6.Determine Array Size / Length ?
length  function for --> Vectors
size function for --> Matrix
>>I = eye(5)
>>size(I)
>>v =[1,2,3,4];
>>length(v)



7.Reshaping the arrays ?
Script :
v = [8 3 4 1 5 9 6 7 2 2 1 5];
Rex = reshape(v,3,4)
Rex2 = reshape(v,4,3)
Rex3 = reshape(v,6,[])
v2 = Rex3(:)

Thursday, April 16, 2020

MATLAB Basics Part 1


1.What is MATLAB ?

To solve Math & Engineering problems this is been created to using syntax like common science & engineering notation. 
--Cleve Moler [Professor] built this for their students

2.Which fields this is been used ?
  • Machine Learning
  • Robotics
  • Image Process
  • Biology
  • Engineers & Scientists also uses to develop
    • Automation
    • Medical Devices
    • Solar Systems
  • etc..
3.Is MATLAB is case sensitive ?
Yes,Variables & Function names are case sensitive.
Note : If observes,So even though generally quotes MATLAB is case sensitive programming language is not completely ,Like below where allows case insensitive 
  • Property Names
  • Scripts and Functions stored in a MATLAB file with a .m extension in Windows 
4.strcmp VS strcmpi ?
Both compares the string and "strcmpi" ignores the case while comparing.

5.MATLAB Variables ?
Ex: pi -->Default function

6.Write the expression for below example ?
>> a=3;
>> b=2;
>> c=-6;
>> x1=(-b+sqrt(b^2-4*a*c))/(2*a)

x1 =

    1.1196

>> x2 = (-b-sqrt(b^2-4*a*c))/(2*a)

x2 =

   -1.7863

7.Functions & Vector Array declaration ?


8.plot(x,y) function example?xlim,ylim functions use?How to increase the number of elements of X & Y axis ?
-->xlim,ylim sets the axis limits
-->As you observe the plot is getting more line elements based on available space,So it will auto selects the points linspace based on the available space. linspace also a function can set custom value




9.linspace function ? Uniformly Spaced Vectors example ?
Ex:Find temperature in Centigrade and Fahrenheit
10.How to add the comments ?
%{Comment Text}%

11.What is logical Vector ?How logical Vector access original Vector ?

Logical Vector is used to access only specific data based on certain conditions.
Script :
>> %{Original Vector}%
>> data = [0.01;0.02;0.01;0.27;0.39;0.01;0.65;0.50];
>> %{Logical Vector}%
>> ind = data > 0.1;
>> activity =data(ind);
>> %{Get the result from Original Vector with below condition}%
>> ind = data <=0.1;
>> %{Update the original vector data - values<=0.1 to 0 }%
>> data(ind) = 0;


12.Element Wise Vector Operations (*  /  ^) ?
Script :
>>v1 = [1,2,3,4];
>>v2 = [2,4,6,8];
>>M =  v1.*v2;
>>D =  v2./v1;
>>S =  v1.^v2;
>>A =  v1+ v2;
>>Sub =  v2- v1;

1.For all the operation Vector Dimensions should be same
2.Should precede by  '.' for these vector operations  like  v1.*v2, v1 ./v2 , v1 .^v2


13.Smooth plotting with more number of elements ?
Script :

>>%{f(x) = 3x^2 + 2x -6}%
>>x = -2:0.1:2;
>>y = 3*x.^2 + 2*x -6;
>>plot(x,y);




13.Vector Transpose ?
Rows to Columns & Columns to Rows
Script :
>>%{Vector Transpose}%
>>v = [2 -1 8.5 6 19];
>>c = v';

                     

Wednesday, April 15, 2020

Matlab vs Octave

For the newbie of  machine learning there is always confusion which scientific computing languages is best to start with?

  1. Matlab & Octave both are very powerful scientific computing languages
  2. Matlab provides variety of tools which helps users ease their task & It's a licenced version.Free trail is available.
  3. Octave is most powerful to solve linear & non-linear problems numerically,It's free,Open-Source scientific computing language available for use.
Summary : Each has its own high & low,Below is my view.
  1. Time VS Money 
    • Matlab which has variety of  tools saves implementation time but costs the money,Most of the enterprises ready to get licences
    • Octave(Free) saves money but implementation takes time compared to Matlab,In many scenarios we will be going with this to save the budget and try something new [Research]
  2. To look at career aspect ,Matlab is widely used in the current industry ,So better to start with this to grab best career opportunity.
  3. Since Matlab is widely used will get better help from community / documentation to pick quickly the learning curve.
  4. Over all if we are master at one of these language easy to switch other based on need,There will be syntax difference.
Matlab (matrix laboratory) developed by MathWorks. MATLAB allows matrix manipulations, plotting of functions and data, implementation of algorithms, creation of user interfaces, and interfacing with programs written in other languages.
-->As of 2018, MATLAB has more than 3 million users worldwide


GNU Octave Primarily intended for numerical computations. Octave helps in solving linear and nonlinear problems numerically, and for performing other numerical experiments using a language that is mostly compatible with MATLAB. It may also be used as a batch-oriented language. Since it is part of the GNU Project, it is free software under the terms of the GNU General Public License.