Error in using a function with more that one argument - matlab

Here is the code of the functions I'm using.
function max = find_max( matrix )
a = -1;
for i = 1:numel( matrix ),
if ( matrix(i) > a),
a = matrix(i);
end
end
max = a;
end
function maxs = find_maxs( matrix, count )
maxs = [];
while (count > 0),
a = -1;
for i = 1:numel( matrix ),
if ( matrix(i) > a && ~is_present(maxs, matrix(i))),
a = matrix(i);
end
end
maxs = [maxs a];
count = count - 1;
end
end
function present = is_present( vector, element )
for i = 1:numel( vector ),
if ( vector(i) == element),
present = TRUE;
return;
end
end
end
When I try to call:
m = [1 2 3 4];
disp(is_present(m, 1));
Or the function find_maxs, I get this error.
??? Undefined function or method 'is_present' for input arguments of type 'double'.
I'm new to matlab and I don't understand why I'm getting this error. The name of the file is find_max.m, the same name of the first function (which works fine).

Just to expand on eigenchris's comment (I'd put it as a comment but I don't have the privilege yet),
Each function should have it's own m file, and the m file should have the same name as the function.
Ex:
function max = find_max( matrix )
should be in a file named 'find_max.m'

Related

Merge sort in scilab

I implemented merge sort in scilab with the following code:
function x = mergesortre (x)
n = length (x);
if ( n > 1 ) then
m = floor (n/2);
p = n-m;
x1 = mergesortre ( x(1:m) );
x2 = mergesortre ( x(m+1:n) );
x = merge ( x1 , x2 );
end
endfunction
function [x] = merge ( x1 , x2 )
n1 = length (x1);
n2 = length (x2);
n = n1 + n2;
x = [];
i = 1
j = 1
k = 1
while(j<=n1 && k<=n2)
if x1(j)>=x2(k)
x(i)=x2(k);
k=k+1;
i=i+1;
elseif x1(j)<x2(k)
x(i)=x1(j);
j=j+1;
i=i+1;
end
end
if (j > n1) then
x(i+1:n) = x2(k:n2);
else
x(i+1:n) = x1(j:n1);
end
endfunction
a=[5,4,3,2,1];
x=mergesortre(a);
disp x;
However in when i try to see the sorted array using the terminal window its showing only the first element,for example if my array is [5,4,3,2,1] its only giving output as 1. I need help understanding what i did wrong.
Your error is in the merge function. As i is already incremented at the end of the loop you have to concatenate starting at i:
if (j > n1) then
x(i:n) = x2(k:n2);
else
x(i:n) = x1(j:n1);
end
A recursive algorithm needs a termination condition. There is no such condition in your function mergesortre. It's likely because the declaration of your function does not used the input argument x. Try to change it like this:
function [a] = mergesortre ( a )
I guess you wanted to use a as an input and output argument.

Convesion vector Index exceeds array bounds - matlab

I build some program in Matlab for "Histograma matching".
When I'm trying to implement the function "conVector" I get the error
"Index exceeds array bounds." anyone can help me with this error?
Here is my full code. Thank you!
function [newImage] = histShape (srcimg,destimg)
%find the histogram of the image
src = imgHist(srcimg);
dest = imgHist(destimg);
sna = normalizationHist(src);
dna = normalizationHist(dest);
conVector(sna,dna);
end
function [Hist] = imgHist (img)
[Rows,Cols] = size(img);
Hist = zeros(1,256);
for i=1:Rows
for j=1:Cols
Hist(img(i,j)+1)=Hist(img(i,j)+1)+1;
end
end
end
function [Ahist] = normalizationHist (hist)
[Rows,Cols] = size(hist);
Ahist = hist;
for i=2:256
Ahist(i)=Ahist(i-1)+hist(i);
end
Ahist = Ahist/(Rows*Cols);
end
function [cv] = conVector(SNA,DNA)
cv=zeros(1,257);
s = 1;
d = 1;
while s<=256
if DNA(d)<SNA(s)
d = d+1;
else
cv(s)=d;
s = s+1;
end
end
end
If all values in DNA(d:end) are smaller then the value in SNA(s) than the loop keep adding 1 to d but not to s, and finally goes out of bound because it conditioned only by s.
I guess you should either take the s = s+1 out of the inner condition, so it will be executed on every iteration, or add a condition on d to the loop, or convert it to a for loop.

MATLAB Morse Code diff and find functions

We are trying to write a function that takes arr and counts how many 0's and 1's appear in sequence. The output should be a 2D array where column 1 is how many appear in sequence and column 2 is which token it is (0 or 1).  Our function below
function [token] = tokenizeSignal(arr)
matA = diff(find(diff([log_vector;-1])));
addA = zeros(size(matA, 1),1);
matA = [matA, addA];
matB = diff(find(diff([log_vector;0])));
addB = ones(size(matB, 1), 1);
matB = [matB, addB];
[nRowsA, nCols] = size(matA);
nRowsB = size(matB, 1);
AB = zeros(nRowsA + nRowsB, nCols);
AB(1:2:end, :) = matA;
AB(2:2:end, :) = matB;
token = AB;
works with
arr = [0; 0; 0; 1; 1; 1; 0];
but nothing else because it adds random integers into the matrix. Why does it do this and how can I fix it?
Here is code that takes any array arr and produces what you want:
% input checking/processing
% ... convert the input into a column vector
arr = arr(:);
% ... check that the input is nonempty and numeric
if ~isnumeric(arr), error('Bad input'); end
if isempty(arr), error('Bad input'); end
% determine the starting indices of each sequence in arr
I = [1 ; find(diff(arr)) + 1];
% determine the values of each of these sequences
values = arr(I);
% determine the length of each of these sequences
value_counts = [diff(I) ; length(arr) - max(I) + 1];
% produce the output
token = [value_counts, values];

How to specify function signature for function handles in MATLAB code generation?

I want to compile a MEX from a function that has the following code (MATLAB R2015a):
function r = MyFunc(x,type)
ind = randi(numel(x), 1);
getInd = #getIndFixed;
if strcmpi(type, 'random')
ind = numel(x);
getInd = #getIndRandom; % error here
end
x(getInd(ind)) = 1;
end
function k = getIndFixed(n)
k = n;
end
function k = getIndRandom(n)
k = randi(n, 1);
end
I get the type mismatch error between getIndFixed and getIndRandom at the line specified above:
Type mismatch: function_handle getIndFixed ~= getIndRandom.
Is there a way around this problem?
For instance, a way to specify that both functions have the same signature?
In C, the signature of the function would be:
int (*getInd)(int);
int getIndFixed(int);
int getIndRandom(int);
//...
getInd = getIndFixed;
getInd = getIndRandom;
You cannot change function handles to different functions after they are assigned in code generation. Unlike "C" these function calls are resolved at compile time. If your type input is constant then you can write your code as
function r = MyFunc(x,type)
if strcmpi(type, 'random')
ind = numel(x);
getInd = #getIndRandom; % error here
else
ind = randi(numel(x), 1);
getInd = #getIndFixed;
end
x(getInd(ind)) = 1;
end
If type is not constant you need to move everything inside if or else branch.

how to convert from type logical to matrice

function MatB = MBin_Init
N=6;
P=4;
SM=1;
while(SM>0)
MatBin=rand(P,N)>=0.5;
sv = sum(MatBin,2)==0;
SM = sum(sv);
end
MatB.MatBin = MatBin ;
end
This function create a random binary matrices of size(P,N).
Then i have created an other to search if there is a colomn of zeros in matrix1 at position p, if yes then matrix2 take a column of zeros too in the same position p.
function [ MatB1 ] = Replace( )
MatBin1 = MBin_Init();
MatBin2 = MBin_Init();
indexColonneNulle = 0;
for i=1:4
if sum(MatBin1(:,i)) == 0
indexColonneNulle = i;
end
end
if(indexColonneNulle > 0)
MatBin2(:,indexColonneNulle) = 0;
end
MatB1.MatBin2 = MatBin2;
end
An error appear when executing these functions!!
Undefined function 'sum' for input arguments of type 'struct'.
Error in Replace (line 6)
if sum(MatBin1(:,i)) == 0
What i have to do please??
thanks