Why has my function returned zero?

조회 수: 22 (최근 30일)
Daniel Fonsêca
Daniel Fonsêca 2018년 12월 6일
편집: Walter Roberson 2023년 3월 27일
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
Manuel Infante Francés 2023년 3월 26일
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
Manuel Infante Francés 2023년 3월 27일
편집: 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