Why has my function returned zero?

20 views (last 30 days)
Daniel Fonsêca
Daniel Fonsêca on 6 Dec 2018
Edited: Walter Roberson on 27 Mar 2023
I have a functio which has returned zero sometimes when it must not. I saw the function and it is running normal, but when it pass the values to variables, they are zero.
A is a column vector with more than 1000 lines. This is the input
function [amf,pf,wf]=la(A)
%pks vetor com os picos
%locs vetor com as localizações
%w vetor com as larguras
[pks,locs,w] = findpeaks(A,'Annotate','extents','WidthReference','halfheight');
%'findpeaks' função que indica os picos e comprimentos dos picos.
tm = size(pks);
m = 0;
%Variáveis usadas para armazenar as amplitudes (am), picos (p) e larguras (we).
am1 =0;
p1 = 0;
we1 = 0;
am2 =0;
p2 = 0;
we2 = 0;
for x = 1:tm(1,1) %Analisando os picos maiores que 1000
if 1000 < pks(x,1)
m = m+1;
am1(m,1) = pks(x,1);
p1(m,1) = locs(x,1);
we1(m,1) = w(x,1);
end
end
%Agrupando os picos ao redor do maior valor
v=0;%indica a quantidade de elementos formando o pico
u=1; %indica a quantidade de picos
indice=0; %Armazena os indices dos picos
tm = size(p1);
for x=tm(1,1):-1:2 %For para analisar as difrenças de posições
a= p1(x,1) - p1(x-1,1);
if a<200 %Se a diferença for menor que 200 canais, agruparemmos os indices na variável indice
v=v+1;
indice(v,u)= x;
indice(v+1,u) = x-1;
else
u=u+1; v=0;
end
end
tm=size(indice);
if tm(1,1)>1
%For para analisar qual maior pico dentro de um grupo
for u=1:tm(1,2)
for v=1:tm(1,1)-1
i = indice(v,u);
if am1(i,1)<am1(i-1,1)
am2(u,1)=am1(i-1,1);
p2(u,1)=p1(i-1,1);
we2(u,1)=we1(i-1,1);
end
end
end
end
tm=size(am2);
%analisando entre os picos, qual é o último
if tm(1,1)>1
for x=1:tm(1,1)-1
if p2(x,1)<p2(x+1,1)
amf=am2(x+1,1);
pf=p2(x+1,1);
wf=we2(x+1,1);
end
end
else %Caso tm(1,1) seja igual a 1
amf=am2(1,1);
pf=p2(1,1);
wf=we2(1,1);
end
end
  3 Comments
Rik
Rik on 6 Dec 2018
Everything depends on your input. Please make sure we can independently run your code and reproduce the issue.
madhan ravi
madhan ravi on 8 Dec 2018
upload A(input) as a text file to test

Sign in to comment.

Answers (2)

Cris LaPierre
Cris LaPierre on 7 Dec 2018
Edited: Cris LaPierre on 7 Dec 2018
Is your code returning 0 or empty? Since we don't know anything about your input, here are some suspicious things to look into.
You create am2, p2, we2 and set them equal to zero at the top. There are two obvious scenarios when 0 is returned. When tm(1,1) == 1. This happens when size(p1,1) < 3 or when size(am2,1) == 1.
I also see that your code does not assign any values when p2(x,1)>=p2(x+1,1).
if p2(x,1)<p2(x+1,1)
That might be another area to investigate.

Manuel Infante Francés
Manuel Infante Francés on 26 Mar 2023
To me the same thing happens to me. I have a matrix A of (6x501) and I want to get a vector (3006x1) with all the columns of A next to each other. The last row of all the columns of A are negative numbers and the function returns zeros in the rows that are multiples of 6 of the obtained vector. I attach the code. I would appreciate any help in that regard.
function y = fcn(a,b,c,d,e,f)
y=[a b c d e f]';
  13 Comments
Walter Roberson
Walter Roberson on 27 Mar 2023
function y = fcn(a,b,c,d,e,f)
assert(isfloat(a), 'a must be float')
assert(isfloat(b), 'b must be float')
assert(isfloat(c), 'c must be float')
assert(isfloat(d), 'd must be float')
assert(isfloat(e), 'e must be float')
assert(isfloat(f), 'f must be float')
y=[a b c d e f]';
y=y(:);
end
Manuel Infante Francés
Manuel Infante Francés on 27 Mar 2023
Edited: Walter Roberson on 27 Mar 2023
Thank you Mr. Walter. Indeed, the solution provided by you gives the desired value. But, I must say and also thank Mr. Torsten's solution:
function y = fcn(a,b,c,d,e,f)
y=[a b c d e f]';
y=y(:);
end
After reviewing the model many times, I have realized that the vector (f) that entered the function was not the one that should enter (supposedly with negative values) but one with values equal to zero. My apologies for the confusion and thanks again.

Sign in to comment.

Categories

Find more on Function Creation in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!