In matlab variables are saved in workspace all through matlab session. While manipulating data at the command line, the variable are stored in base workspace. Variables in workspace can be displayed using whos command. In matlab characteristic has their personal workspace, become independent from matlab workspace.
A Matlab variable is basically which you will assign a value while that value stays in memory. Where the value which was available in the memory so that you can read the programs and operates it on different types of data, and stores back it into memory.
Basically their three types of variables in matlab:
Local Variable:
A local variable which is described with inside the function. It can only used inside the block code in which it is declared. The local variable exists till the block of the function is below execution. After that, it will be destroyed automatically.
Global Variable:
A global variable is a program defined outside the function. The scope of global variable, it holds the value through the lifetime of the program.it stores fixed value decided by the complier. Example of global variable:
Function setGlobalx (val)
global x
x=val;
end
Persistence Variable:
Persistent variables can be used inside a simplest function only. Persistent variables stay in memory till the M-document is cleared or changed. Persistent is precisely like global, besides that the variable name is not in the global workspace, and the value is reset if the M-document is changed or cleared.
Both the global and local variable are more significant in programming, global variable occupy large memory because of large amount of variables. Therefore, it is beneficial to avoid declaring undesirable global variables.
Where in switch case from available number of alternative it execute one set of statement. In this each alternative is considered as a case, switch can handle multiple conditions in a single case statement by enclosing the case expression in a cell array.
This includes the phrase in any other case, observed with the aid of using the statements to execute if the expression’s cost isn’t treated with the aid of using any of the previous case groups.
In its fundamental syntax, switch executes the statements related to the primary case wherein case. When the case expression is a cell array (as with inside the second case above), the case suits if any of the factors of the cell array suits the transfer If no case expression suits the switch expression, then manipulate passes to the in any other case. After the case is executed, application execution resumes with the assertion after the end. If the first case is true then it does not execute next state. So, break statement does not required.
Switch statements
Case cost1
Declarations % executes if statements is cost1
Case cost 1
Declarations % executes if statements is cost 2
Otherwise
Declarations % executes if statements does not match any case
End
For example let consider to execute a certain block of code based on what the string ‘color’ is set to:
color = ‘rose’;
switch lower(color) case {‘red’, ‘light red’, ‘rose’} disp(‘color is red’)
case ‘blue’ disp(‘color is blue’)
case ‘white’ disp(‘color is white’)
otherwise disp(‘Unknown color.’) end
BREAK:
Break statements is used to exist early from for and while loops. Where in nested loop break is used only for inner loop only, after the execution of the break statement control passes statements that follow the end of that loop. For nested loop if we use break statement only it stop executing inner loop, not outer loop, control passes statements that follow the end of that loop.
Let’s consider an example: y = [-2 -4 0 -4 3 7];
% let us test each element with some special condition for i = 1 : length(z)
% let we test for the given condition which is greater-than-zero value if z(i) > 0 % let we terminates the loop execution break end y = z(i) + 5; disp(y)
end
CONTINUE:
Continue statement temporary interrupts the program execution loop, skipping any given remaining statements in the body of the loop and continues with the current pass. Let’s consider an example. % Let’s us consider an example and execute the example based on assumption that you are verifying a set of values. for i = 1: 9 if (i == 2 | i == 3) continue end
% here we can continue with the remaining values, disp (i)
Loop or iteration are useful for execution of the sequence more than once of the given statements. Basically their two types of loop forms they are: while loops and for loops. Main different between these two loops is how repetition took place in both while and for loops. While loop is used when how many times you want the loop to execute it is done based on the given condition. When going to for loop the number of repetition are given before the loop start execution.
WHILE LOOP:
While loop executes the group of statements are statement indefinite number of times until the logic condition become true. Syntax of while loop:
While expression
Statements
end
For example, suppose you wanted to double the number 2 over and over again until you got to a number over 1,000,000
number = 2;
counter = 1;
while number <= 10^6
number = 2*number;
counter = counter + 1;
end
FOR LOOP:
For loop is used for execution of group of statements or a statement for predefined number of times. Syntax of for loop.
For index = start: increment: end
Statements
End
Where index is the loop control variable
For example
s = 0;
For i = 1:10 %increment of 1
s = s + i;
End
disp(s);
FLOW CONTROL:
IF, ELSE AND ELSEIF:
If statements evaluate the logical expression and execute the group of statements when the expression is true, otherwise for execution of alternate statements it go with else or else if. An end keyword, which matches the if, terminates the last group of statements. If the logical expression is true (1), MATLAB executes all the statements between the if and end lines. It resumes execution at the line following the end statement. If the condition is false (0), MATLAB skips all the statements between the if and end lines, and resumes execution at the line following the end statement. Syntax of if condition.
if logical_expression
Statements
End
The else if statement has a logical condition that it evaluates if the preceding if (and possibly elseif condition) is false (0). The statements associated with it execute if its logical condition is true (1). You can have multiple elseifs within an if block. Syntax of elseif condition.
In bit-wise operator logical bit-wise operation is performed for positive integer input, where the input may be scalar or array. Below shows some functions of bit-wise operator.
Bitand it performs the bit-wiseAnd operation on two positive integers.
For example: A = 28; % binary 11100 B = 21; % binary 10101
bitand (A, B) = 20 (binary 10100)
Bitor it performs the bit-wiseor operation on two positive integers.
For example: A = 28; % binary 11100 B = 21; % binary 10101
bitor(A,B) = 29 (binary 11101)
Bitcmp It returns the bit-wise complement as an n-bit number, where n is the second input argument to bitcmp.
For example: A = 28; % binary 11100 B = 21; % binary 10101
bitcmp(A,5) = 3 (binary 00011)
Bitxor It returns the bit-wise exclusive OR of two nonnegative integer arguments.
For example: A = 28; % binary 11100 B = 21; % binary 10101
Bitxor (A,B) = 9 (binary 01001)
SHORT CIRCUIT OPERATOR:
Short-circuit operator used when first operand does not determine the output fully we consider the short-circuiting operator to calculate the second operand.
&& It returns logical 1 (true) if both inputs calculate to true, and logical 0 (false) if they do not.
|| It returns logical 1 (true) if either input, or both, calculate to true, and logical 0 (false) if they do not.
RELATIONAL OPERATOR:
Relational operator performs element by element comparison between two arrays. They return a logical array of the identical size.
Applying rules and Introduction to Production System
Here in this case I have applied only one rule (among four i.e., moving right, left, up and down) which is closer to goal. In practical, all the rules are applied and selects the best outcome which is closer to the goal and continues the generation of new state till the goal state is achieved.
2
8
3
1
6
4
7
5
Intial state
↓
2
8
3
1
4
7
6
5
Moved the empty tile to up .Therefore 6 comes to down position
↓
2
3
1
8
4
7
6
5
Here the empty tile is moved up. Therefore 8 comes down
↓
2
3
1
8
4
7
6
5
Here the empty tile moved left. therefore 2 comes to right
↓
1
2
3
8
4
7
6
5
Here the empty tile moves down .Therefore 1 comes up
↓
1
2
3
8
4
7
6
5
Here the empty tile moves right. Therefore 8 comes left.
Here my goal position is achieved. Starting from a start position and which was part of a space of the problem state which we have seen is a factorial 9. We could go and look at different configurations on the way generating lot of them and arrived at the goal position ‘D’ from a start position ‘s’. This paradigm of working like this is problem solving as state space search.
Production Systems and AI:
A production system consists of
1. Database
2. Operations
3. Control components.
Here this database is different from data based systems. Here database refers to set of rules and facts. Operations and Control components are the building blocks for constructing lucid descriptions of AI systems. Several AI systems exhibit little or high rigid isolation among the computational components of data, operations and control.
Production system involves an isolation of these computational components and thus seem to capture the essence of operation of most AI systems. Selecting rules and monitoring of those sequences of rules constitute the control strategy for production systems.
Description of Problems in AI consists of 4 states as below
1) State Space
2) Initial State
3) Final State
4) Operators
State Space
State space: The state space is defined as the set of all possible configurations of a relevant object.
Initial State
Initial state: Initial state specifies one or more states within that space as a possible situations from which the problem solving starts.
Final State
Final state: Final state specify one or more states that could be acceptable as solutions to the given problem.
Operators
Operators: Operators are the set of rules that describe how we are going to move from one state to other.
The above is the formal description of problem solving a state space search First we have to define state space i.e., all the possible configurations. We then identify one or more states within the space as possible situations from which we can start. This is the initial state from which one or more states from the problem space that are acceptable as solutions to the given problem is referred as final state operators have two portions one is right and the other is left.
As we know that operators are the set of rules which describes how we are moving from one state to other This information regarding the move must be true. For suppose in the above example of 3*3 matrix. In this case we have 8 tiles with information and one with empty space this empty space is a problem state. Some rules are specified to this problem state like move right or left or up or down.
Here if we apply any one among the rules lets say like to move right. It can be done and by this action new state space is generated. The second rule is to move left here in this case the second rule is not applicable. So when we specify the rules that describes actions it must be true for the action to take place needs to be looked at first and then the action can take place and generates new state space.
Initial state as an 8*8 array. 2 legal moves as set of rules; each rule consisting of two parts.
A left side serves as pattern that has to be matched.
A right side that describes the change to reflect the move.
Destination state as any board position where opponent does not have a legal move. Therefore its a win. When we start playing chess by starting at an initial state using a set of rules to move from one state to another we end up in one of the set of final states. This state space representation seems natural for games such as chess, 8 puzzle because of the state problems with the use of more complex structures than a matrix. Here for a 8 puzzle game we used 33 matrix and for chess a 8*8 matrix comes into mind immediately. This kind of state space representation also used for other similar problems with less structures. But in case of small structures to use state space representation for this type of structures we require more complex structures than a matrix.
Description Of a Problem
General description of problem involve the following.
State space consists of all the problem solving tasks.
State space: A state space consists of all the states ,set of operators that shifts from one state to another.
States are also known as nodes in a connected graph and the edges are denoted as operators. Problem solving can be formulated as search in state space. For example to find the solution of an 8 puzzle game which is represented as 3*3 matrix.
1
2
3
4
5
6
7
8
One of the tile is unoccupied by moving this tile to other positions we can create new states. For example if I move this unoccupied block to right then it becomes
1
2
3
4
5
6
7
8
New state
Here we can create ‘n’ number of states. The number of new states generated all together constitutes to a state space.
All of these states of the domain and all of these operators. In this case it could be 4 operators they are the tile can move towards right, left ,up and down. All these operators together constitute to asset of operators. Once the set of operators and actions are defined then move on to finding solution.
For suppose there are two states start state and end state. If the path from start state to end state is found then the problem is solved. State space is defined as the set of all possible configurations that one get from the relevant objects. This is also referred to problem space.
Let us take previous example 3*3 matrix having 8 elements and one empty space. This empty space is a problem state. Suppose the first matrix is start and the second is destination, the path from ‘S’ to ’D’ the total number of configurations is 9!.
An operator is an image that tells the compiler to carry out numerous numerical or logical manipulations. MATLAB is designed to perform especially on complete matrices and arrays.
Arithmetic Operation:
There are specific form of mathematics operators in matlab they are:
Matrix mathematics operation.
Array mathematics operation.
Arithmetic operator are used to carry out matrix manipulation, it used for performing smooth mathematics operation which is probably together with numbers, subtraction, division, Array mathematics operations are executed detail via way of way of detail, and may be used with multidimensional arrays, each operands need to be the identical size.
Matrix operations observe the rules of linear algebra. By contrast, array operations execute detail via way of way of detail operations and help multidimensional arrays. The character (.) defines the array operations from the matrix operations.
+Addition. A+B adds A and B. A and B must have the identical size, except one is a scalar. A scalar may be added to a matrix of any size.
- Subtraction or unary minus. A-B subtracts B and A. A and B must have the identical size, except one is a scalar. A scalar can perform multiplication of a matrix of different or same size.
* The matrix multiplication of fabricated from matrices may be calculated with the formula, C = A*B.
For nonscalar A and B, the fashion of columns of A want to identical to the fashion of rows of B.
.*Array multiplication or Element-sensible multiplication, A.*B is the detail-via way of way of-detail fabricated from the arrays A and B.A and B want to have the identical size.
/Matrix right division, B/A is quite same as B*inv(A). they will more precisely, B/A = (A'\B')'.
./ Array right division. A./B is the matrix with elements A(i,j)/B(i,j).Aand B must have the identical size, except truly considered one in every of them is a scalar.
.\
Array left division. A.\B is the matrix with elements B(i,j)/A(i,j).A and B must have the identical size, except truly considered one in every of them is a scalar. .\
.\ Array left division. A.\B is the matrix with elementsB (i,j)/A(i,j). A and B must have the identical size, except truly considered one in every of them is a scalar.
' Matrix transpose. A'is the linear algebraic transpose of A. For complicated matrices, that is the complex conjugate transpose.
.' Array transpose. A.' is the array transpose of A. For complicated matrices, this does not comprise conjugation.
Logical Operators
Logical operator performs logical operation and provides it output in the form of boolean i.e., true or false. Matlab has three types of logical operators they are:
Element-wise operator.
Bit- wise operator.
Short-circuit.
Element wise operator performs element wise logical operation on the input same sized output array.
& Logical AND It returns 1 for every element location that is true (notzero) in both arrays and 0 for all other elements. For example.
A = [0 1 1 0 1]; B = [1 1 0 0 1];
A & B = 01001
| Logical OR It returns 1 for each detail area this is real (nonzero) in every one or the wonderful, or each arrays, and zero for all wonderful elements. For example
A = [0 1 1 0 1]; B = [1 1 0 0 1];
A| B=11101
~ Logical NOT It complements the input array of each given element, A.
A = [0 1 1 0 1]; B = [1 1 0 0 1];
~A = 10010
Xor It returns 1 for every element location that is true (notzero) in only one array, and 0 for all other elements.
In matlab it as four signed and unsigned integer classes. Signed work for positive and negative values but wide range of numbers cannot represented as unsigned type.
LOGICAL OPERATIONS:
Itconsist of two values i.e., true or false.
It is used to test state or for relational condition.
It useful for array indexing.
Inefficiency of two dimensional arrays.
CHARACTER AND STRING:
Data type into text.
Native or Unicode.
Which is used to convert to/from numeric.
For multiple strings.
FLOATING POINT NUMBER:
It is default numerical type in matlab.
To show range of values it uses realmin and realmax.
Two-dimensional array can sparse.
Floating point number is represented in Double and single precision in matlab.
In matlab default is double but using some techniques we convert into single precision with simple conversion.
STRUCTURES:
It require more memory for overhead.
Method of passing function arguments.
It can access one or all fields in single operation.
CELL ARRAYS:
Cells useful for storing varying classes and size of array.
It requires more memory for overhead.
In order to handle elements similarly as numeric or logical arrays.
To package data as you want it allows you the freedom.
TABLES:
Rectangular container will manipulate the mixed-type and column-oriented data.
Row names and variable names will identify contents.
We can use Table Properties in order to store metadata such as variable units.
In order to handle elements similarly as numeric or logical arrays.
We can access data in numeric or named index form.
FUNCTION HANDLES:
Pointer to a function.
Enables passing a function to another function.
It used to call functions outside for usual scope.