Problem to add element to matrix in if-else condition in Matlab - matlab

I'm writing a simple program to implement Artificial Neural Network to recognize handwritten characters using Matlab. I used following code to set Target data according to different characters.
Here is a part of code:
load('dataset.mat')
Target_Set=zeros(2,400); %Initialize Target array with 0s
%Set dimensions to resize
h = 50;
w = 45;
imgSize = h*w;
for j=1:4
for i=1:10
for k=1:10
Temp_Struct = struct('im',imresize(handwriting(i,j,k).im,[h,w]));
n = 100*(j-1) + 10*(i-1) + k;
P_Set(1:imgSize,n) = reshape(Temp_Struct.im,[imgSize,1]);
%Set Target patterns...
if (j==1) %When character I % Target Patterns...
Line 19========>Target_Set(0,n) = 1; % I J K L
Target_Set(1,n) = 0; % 0 1 0 1
elseif(j==2) %When character J % 0 0 1 1
Target_Set(0,n) = 1;
Target_Set(1,n)=0;
elseif(j==3) %When character K
Target_Set(0,n) = 0;
Target_Set(1,n)=1;
else %When character L
Target_Set(0,n) = 1;
Target_Set(1,n)=1;
end;
end;
end;
end;
When I run this, I got some error like this.
I couldn't understand why that kind of error occur in line 19. Welcome for any help.

Arrays in Matlab start at index number 1. So it is complaining about your use of a 0 value as an array index.
It should be correct like this.
Target_Set(1,n) = 0;

Related

Output 1, 0.5, or 0 depending if a matrix elements are prime, 1, or neither

I am sending a matrix to my function modifikuj, where I want to replace the elements of the matrix with:
1 if element is a prime number
0 if element is a composite number
0.5 if element is 1
I dont understand why it is not working. I just started with MATLAB, and I created this function:
function B = modifikuj(A)
[n,m] = size(A);
for i = 1:n
for j = 1:m
prost=1;
if (A(i,j) == 1)
A(i,j) = 0.5;
else
for k = 2:(A(i,j))
if(mod(A(i,j),k) == 0)
prost=0;
end
end
if(prost==1)
A(i,j)=1;
else
A(i,j)=0;
end
end
end
end
With
A = [1,2;3,4];
D = modifikuj(A);
D should be:
D=[0.5, 1; 1 0];
In MATLAB you'll find you can often avoid loops, and there's plenty of built in functions to ease your path. Unless this is a coding exercise where you have to use a prescribed method, I'd do the following one-liner to get your desired result:
D = isprime( A ) + 0.5*( A == 1 );
This relies on two simple tests:
isprime( A ) % 1 if prime, 0 if not prime
A == 1 % 1 if == 1, 0 otherwise
Multiplying the 2nd test by 0.5 gives your desired condition for when the value is 1, since it will also return 0 for the isprime test.
You are not returning anything from the function. The return value is supposed to be 'B' according to your code but this is not set. Change it to A.
You are looping k until A(i,j) which is always divisible by itself, loop to A(i,j)-1
With the code below I get [0.5,1;1,0].
function A = modifikuj(A)
[n,m] = size(A);
for i = 1:n
for j = 1:m
prost=1;
if (A(i,j) == 1)
A(i,j) = 0.5;
else
for k = 2:(A(i,j)-1)
if(mod(A(i,j),k) == 0)
prost=0;
end
end
if(prost==1)
A(i,j)=1;
else
A(i,j)=0;
end
end
end
end
In addition to #EuanSmith's answer. You can also use the in built matlab function in order to determine if a number is prime or not.
The following code will give you the desired output:
A = [1,2;3,4];
A(A==1) = 0.5; %replace 1 number with 0.5
A(isprime(A)) = 1; %replace prime number with 1
A(~ismember(A,[0.5,1])) = 0; %replace composite number with 0
I've made the assumption that the matrice contains only integer.
If you only want to learn, you can also preserve the for loop with some improvement since the function mod can take more than 1 divisor as input:
function A = modifikuj(A)
[n,m] = size(A);
for i = 1:n
for j = 1:m
k = A(i,j);
if (k == 1)
A(i,j) = 0.5;
else
if all(mod(k,2:k-1)) %check each modulo at the same time.
A(i,j)=1;
else
A(i,j)=0;
end
end
end
end
And you can still improve the prime detection:
2 is the only even number to test.
number bigger than A(i,j)/2 are useless
so instead of all(mod(k,2:k-1)) you can use all(mod(k,[2,3:2:k/2]))
Note also that the function isprime is a way more efficient primality test since it use the probabilistic Miller-Rabin algorithme.

MATLAB: Limit from n to infinity for power of a matrix

I am trying to compute lim(n->inf) for D^n, where D is a diagonal matrix:
D = [1.0000 0 0 0; 0 0.6730 0 0; 0 0 0.7600 0; 0 0 0 0.7370]
n = 1
L = limit(D^n,n,inf)
This returns the error:
Undefined function 'limit' for input arguments of type 'double'.
I am sure this should result in most entries except the upper-left entry going to zero, but I need to be able to present this with MATLAB results. Is there something else I need to include in my limit function?
If your problem is to compute the inf-limit of a diagonal matrix, you'd better create your own function and handle manually the possible cases :
function Mlim = get_diag_matrix_inf_limit(M)
% get the diagonal
M_diag = diag(M);
% All possible cases
I_nan = M_diag <= -1;
I_0 = abs(M_diag) < 1;
I_1 = M_diag == 1;
I_inf = M_diag > 1;
% Update diagonal
M_diag(I_nan) = nan;
M_diag(I_0) = 0;
M_diag(I_1) = 1;
M_diag(I_inf) = Inf;
% Generate new diagonal matrix
Mlim = diag(M_diag);
end

Swapping rows in a row echelon

I'm trying to make a program that computes, given any matrix A, its echelon form. Here is my code:
function A = myrref(A)
[m,n]=size(A);
for j=1:min(m,n)
A(j,:) = A(j,:)/A(j,j);
for i = j+1:m
A(i,:)= A(i,:)- A(j,:)*A(i,j);
if A(i,i) == 0
row1=A(i,:);
A(i,:)=A(i+1,:);
A(i+1,:)=row1;
end
end
end
It seems to work almost fine, but I still have a problem when swapping rows. For instance, when trying to get echelon form of matrix A=[1 1 1; 2 2 1; 1 2 2], I obtain [1 1 1; 0.5 1 1; 0 0 -1] which is not what I want. Do I need to add another loop that takes care of the 0.5 in the second row first column?
Firstly, it is simplier to use while loop for j because j is not necessarily growing on each iteration. The leading coefficient is not necessarily located on the main diagonal; when all the elements below the leading 0 are zeros, the leading coefficient position shifts to the right.
Secondly, the leading coefficient should be checked before A(j,:)/A(j,j) (to prevent division by 0)
Thirdly, the temporary is not needed to swap the rows as A([i j],:)= A([j i],:)
swaps the ith and jth rows of A.
Here is my version of myrref:
function A = myrref(A)
[m,n]=size(A);
j= 1; % the row index of the leading coefficient position
k= 1; % the column index of the leading coefficient position
while j<m && k<=n
if A(j,k)==0 % we need to change the row order
zeroindex= find(A(j+1:end,k)~=0); % find nonzero elements below A(j,k)
if isempty(zeroindex)
k= k+1; % there is no such elements; shift to the right
else
% swap the rows
A([j zeroindex(1)+j],:)= A([zeroindex(1)+j j],:);
end
else
A(j,:) = A(j,:)/A(j,k);
for i= j+1:m
A(i,:)= A(i,:)- A(j,:)*A(i,k);
end
j= j+1; k= k+1;
end
end
Just as #percusse said you need to finish the loop also your pivot should only go to m-1
Edit: Added an initial pivot based on #AVK's comment
function A = myrref(A)
[m,n]=size(A);
for i = 1:m-1
if A(i,i) == 0
row1=A(i,:);
A(i,:)=A(i+1,:);
A(i+1,:)=row1;
end
end
for j=1:min(m,n)
A(j,:) = A(j,:)/A(j,j);
for i = j+1:m
A(i,:)= A(i,:)- A(j,:)*A(i,j);
end
for i = j+1:m-1
if A(i,i) == 0
row1=A(i,:);
A(i,:)=A(i+1,:);
A(i+1,:)=row1;
end
end
end

Why does the one-dimensional variant of a 2-d random walk not work?

There is a two-dimensional random walk that one can find here which works perfectly in Octave. However, when I tried to write a one-dimensional random walk program, I got an error. Here is the program:
t=[];
x=[];
for i=1:100000
J=rand;
if J<0.5
x(i+1)=x(i)+1;
t(i+1)=t(i)+1;
else
x(i+1)=x(i)-1;
t(i+1)=t(i)+1;
end
end
plot(t,x)
Here is the error:
error: A(I): index out of bounds; value 1 out of bound 0
Thank you.
No need for a loop:
N = 100000;
t = 1:N;
x = cumsum(2*(rand(1,N)<.5)-1);
plot(t,x)
For the 2D case you could use the same approach:
N = 100000;
%// t = 1:N; it won't be used in the plot, so not needed
x = cumsum(2*(rand(1,N)<.5)-1);
y = cumsum(2*(rand(1,N)<.5)-1);
plot(x,y)
axis square
You get an error because you ask MATLAB to use x(1) in the first iteration when you actually defined x to be of length 0. So you need to either initialize x and t with the proper size:
x=zeros(1,100001);
t=zeros(1,100001);
or change your loop to add the new values at the end of the vectors:
x(i+1)=[x(i) x(i)+1];
Since t and x are empty, therefore, you cannot index them through x(i+1) and x(i).
I believe you should intialize x and t with all zeros.
In the first iteration, i = 1, you have x(2) = x(1) +or- 1 while x has dimension of zero. You should define the starting point for x and t, which is usually the origin, you can also change the code a little bit,
x = 0;
N = 100000;
t = 0 : N;
for i = 1 : N
x(i+1) = x(i) + 2 * round(rand) - 1;
end
plot(t,x)

How to add character to numeric matrix in matlab?

In matlab usually we add a header using fprintf command.
This is a problem when the size of the table depends on the input and when it exceeds a certain range (more than total number of column able to be presented in the command window).
When this occurs the header which we specified previously using the fprintf command will not be compatible with the current output data.
I would like to know is there a way like adding a character string into the 1st row of the matrix during some kind of iteration process. I had tried hardly but still can't find a proper way to solve this issue.
Or it is actually cannot be done in matlab for this purpose.
Eg
clear;clc
A = [2 8 3 1;0 2 -1 4;7 -2 1 2;-1 0 5 2]
B = [-2;4;3;5]
Es = 1e-5
n = length(B);
x = zeros(n,1);
Ea = ones(n,1);
iter = 0;
while max(Ea) >= Es
if iter <= 30
iter = iter + 1;
x_old = x;
for i = 1:n
j = 1:n;
j(i) = [];
x_cal = x;
x_cal(i) = [];
x(i) = (B(i) - sum(A(i,j) * x_cal)) / A(i,i);
end
else
break
end
x_ans(:,iter) = x;
Ea(:,iter) =abs(( x - x_old) ./ x);
end
result = [1:iter; x_ans; Ea]'
for the coding above..how could I add the heading like iteration for 1st column, x1...x2...x3..xn for nth column and error x1..error x2..error xn for another n column. I would like to make this heading can be automated generated based on the input matrix
If the size of the table depends on the input, use a cell array, using c = cell(...).
In each iteration simply call c{i,j} instead of c[i,j].