Why has my function returned zero?

21 次查看(过去 30 天)
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 个评论
Rik
Rik 2018-12-6
Everything depends on your input. Please make sure we can independently run your code and reproduce the issue.
madhan ravi
madhan ravi 2018-12-8
upload A(input) as a text file to test

请先登录,再进行评论。

回答(2 个)

Cris LaPierre
Cris LaPierre 2018-12-7
编辑:Cris LaPierre 2018-12-7
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
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 个评论
Walter Roberson
Walter Roberson 2023-3-27
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
编辑:Walter Roberson 2023-3-27
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by