Monday 28 April 2014

matlab commands



Mean
A=[1 2 3 4;5 6 7 8];
i)                    mean (A)=mean(A,1)=[3,4,5,6]; %column-wise mean
Explanation:
1 2 3 4
5 6 7 8
---------
3 4 5 6
ii)                   mean(A,2)= [2.5,4.5];
1 2 3 4-----(1+2+3+4)/4 = 10/4 =2.5
5 6 7 8-----(5+6+7+8)/4 = 26/4 =4.5

Size
size(A,2) =  4 %number of columns
size(A,1) = 2%number of rows

We Normalize images to reduce the error due to lighting conditions

A=
1 2 3 4
5 6 7 8

A'=
1 5
2 6
3 7
4 8

temp=A'(:);
=
1
2
3
4
5
6
7
8


If A=
1 2
3 4
-----
2 3

here n-1= 2-1=1;
std(A)=

[√ (1-2)2+(3-2)2/(2-1)     √ (2-3)2+(4-3)2 /(2-1) ]     =[ √2/1    √2/1] =  [√2    √2]= [1.414   1.414];

Sum(A,1)=sum(A)=[4 6]
Sum(A,2)=[3 7]%row wise sum

A(:,1).^2
=
1
9


A= 1       B= 4
   2          5
   3          6

A’*B =32

[1 2 3]*  4           4+
          5  =        10
          6           18
                    -------
                      32     

 
img=uint8(image);   %converts to unsigned 8-bit integer. Values range from 0 to 255

Normalize cheyataniki  its own mean tho minus chestamu

Wednesday 23 April 2014

Static and Dynamic allocation in C and C++

Operating system allocates certain amount of memory for each program and it doesn't grow at run time.
This memory is divided into 4 blocks
* heap
* stack ----contains local variables and function stack frames
* static/global variables
* code ---- contains instructions

when a program is executed, certain amout of memory from stack is taken and allocated for main() method. This is called stack frame.
Similarly stack frame is created for each function called above the main()'s stack frame.

when a function is called, it is pushed onto the stack.
when control returns from function, it is popped from stack.
Whatever is on the top of the stack is executing and this is called function call stack.

Also, we need to know the size of local variable at compile time only.

For ex:
int n;
cin>>n
int a[n];   //wrong as we cannot allocate memory to the array at run time.

At the time of program compilation itself, the amout of memory to be allocated for each function's stack frame is decided by the compiler and do not change!!!!

If there is a bug in the program, a recursive function calls itself  indefinitely then the entire stack memory may be filled with function stack frames and  stack may overflow and program crashes!!!!!!!

Hence it is called Static allocation.

Dynamic Memory allocation

if you want memory to be kept as long as you want, instead of getting cleared automatically when popped from stack.
Also if you want allocate the size of an array/object at run time i.e, dynamically

Done using new and delete operators in C++

for ex:
int a;
int *p;
p=new int;   //new allocates memory of size int on the heap.
*p=100;
delete p;   //frees the allocated space.

we need to explicitly remove the memory on heap as it donot get erased!!!!!

Ex:2

p= new int[20];
delete[] p;

In C, dynamic memory allocation is done using
1. malloc
2.calloc
3. realloc

void * malloc(size_t size)
malloc returns a void pointer that has address of first byte of memory of the block of memory allocated on heap , so we typecast as shown below
ex:

int *p;
p= (int *) malloc(20*sizeof(int));

we can also write
p= (int *)malloc(80); but the size of int variable varies from computer to computer..so to make our program robust we donot use this way.

free(p); // to deallocate the memory.

calloc is same as malloc except

1. it takes 2 arguments. For ex: p=(int *)calloc(20, sizeof(int));
2. it initializes all the memory allocated on heap to 'zero'

realloc is used to change the amout of memory previously allocated on heap
syntax:
void * realloc(void * ptr, size_t size)
ptr is pointer to existing block

size is the size of new block

size_t is data type that has only positive values For ex: unsigned integer.

malloc, realloc, calloc can be used in C++ also as C++ is superset of C

Entire content above is the summary of the lecture given in below Link  https://www.youtube.com/watch?v=xDVC3wKjS64

Thursday 17 April 2014

State, Timing and Interaction Overview Diagrams for Library Managment System


State Machine Diagrams
State Chart Diagram for Book

 
Drawn using Umlet

State diagram for Membership

Timing diagrams
Drawn using Visual Paradigm




 
Interaction Overview Diagrams