Why has my function returned zero?

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 Commenti

How can we possibly answer this without seeing your variables and your actual function code?
Rik
Rik il 6 Dic 2018
Everything depends on your input. Please make sure we can independently run your code and reproduce the issue.
upload A(input) as a text file to test

Accedi per commentare.

Risposte (2)

Cris LaPierre
Cris LaPierre il 7 Dic 2018
Modificato: Cris LaPierre il 7 Dic 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 il 26 Mar 2023

0 voti

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 Commenti

What is class() of each of your inputs?
Like this ?
A = [0 4 7; 11 15 19]
A = 2×3
0 4 7 11 15 19
A = A(:)
A = 6×1
0 11 4 15 7 19
Thank you. Yes, but when the values ​​are negative the answer is zero. For example if the elements of the second row are negative [0 4 7;-11 -15 -19], the fourth, fifth and sixth row of the answer are zero [0;11;4;0;0;0]
For example if the elements of the second row are negative [0 4 7;-11 -15 -19], the fourth, fifth and sixth row of the answer are zero [0;11;4;0;0;0]
???
A = [0 4 7;-11 -15 -19];
A = A(:)
A = 6×1
0 -11 4 -15 7 -19
Seems you use a different code. You might want to include it here.
Thank you, but how can I know the class ()? The input vectors [a b c d e f] come from MATLAB Function blocks, which in turn come from seector blocks which in turn come from an extended kalmam filter block.
I wonder if those negative entries happen to be less than 5/10000 and you have "format short" in effect?
format short
A = [0 4 7;-.0011 -.00015 -.000019];
A
A = 2×3
0 4.0000 7.0000 -0.0011 -0.0001 -0.0000
format long g
A
A = 2×3
0 4 7 -0.0011 -0.00015 -1.9e-05
Torsten
Torsten il 26 Mar 2023
Modificato: Torsten il 26 Mar 2023
Manuel Infante Francés comment moved here:
Like this ?
A = [0 4 7; 11 15 19]
A = A(:)
Yes, now:
when I load the data from workspace and execute the code in command window the result is as desired. The problem arises when applying it in Matlab Function of the model in Simulink.
I wonder if those negative entries happen to be less than 5/10000 and you have "format short" in effect?
I indicate the first three columns:
12.2549615216114 12.2500232146314 12.2352847886914
58.6971528224936 58.7743679178343 58.8736987076398
1.11001172339046 1.11000659363486 1.10995585012337
5.88001232319817 5.88000221999864 5.87972551295995
0 0 0
-1.46785244195337 -1.46785804038749 -1.46787014889430
and the first 6 rows of the response:
12.2549615216114
58.6971528224936
1.11001172339046
5.88001232319817
0
0
The sixth row should be (-1.46785244195337) instead of zero
We don't know how you use your function "fcn" to transform your matrix into the column vector.
What about
function Y = fcn(A)
Y = A(:);
end
?
We don't know how you use your function "fcn" to transform your matrix into the column vector.
What about
function y = fcn(a,b,c,d,e,f)
y=[a b c d e f]';
y=y(:);
end
No matter how you put it, the MATLAB function returns negative values ​​with value equal to zero.
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
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.

Accedi per commentare.

Tag

Richiesto:

il 6 Dic 2018

Modificato:

il 27 Mar 2023

Community Treasure Hunt

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

Start Hunting!

Translated by