Borrar filtros
Borrar filtros

find variables in a matrix with multiple conditions

5 visualizaciones (últimos 30 días)
Enzo
Enzo el 6 de Oct. de 2023
Comentada: Enzo el 10 de Oct. de 2023
Hello everyone,
I have an array of elements, lets say A = 1000x1 wich contains only 0,1, 2 and 3 only. I need to find (storee in variables) and plot
  • where the 0 is repeated four or more time in a row (eg. 1 0 0 0 0 0 0 1 0 0 1 2 3). In this examaple, the only sequence to be retained is formed by the six zeros.
  • where there are 2 or 3's repeated for three or more time in a row (eg. 1 0 1 0 1 1 2 3 3 3). In this example the only sequence to be retained is 2 3 3 3
please find the original matrix in the attachments.
for the following example I would like to have this kind of plot:
where the zero's condition is satisfied the plot goes up (let's say plus 1), no condition satisfied the plot stays in the base line (let's say 0), and finally when the 2 and 3's condition is satisfied the plot goes down (let's say at ,minus 1).
Thanks in advance for you help. This community is really amazing

Respuesta aceptada

Dyuman Joshi
Dyuman Joshi el 6 de Oct. de 2023
Editada: Dyuman Joshi el 6 de Oct. de 2023
I have written a function to check for the criteria you have mentioned.
You can also check out this FEX submission for the same - RunLength
%Random input
A = [1 0 0 0 0 0 2 3 1 0 0 0 0 0 0 0 2 3 3 3 0 3 1 2 2 2 2 1 1 1 3 3 3];
%Corresponding outputs
out1 = convert(A,0,4);
out2 = convert(A,[2 3],3);
% +1 for 0
% -1 for [2 3]
out = 1*out1+(-1)*out2;
vec = 1:numel(A);
%plot using stairs to get the square wave form
stairs(vec,out)
grid on
ylim([-2 2])
xticks(vec)
%% Function to check for groups of consecutive apperances above a threshold
function out = convert(in,val,thresh)
%% in - input array (expected to be a vector)
%% val - values to check for as a part of a cluster (expected to be a vector)
%% thresh - threshold for elements in a cluster (expected to be a scalar)
%% out - output (row vector) = 1 for the elements of groups that satisfy the criteria, 0 otherwise
%Check for input
if ~isvector(in)
error('Input array must be a vector');
end
%Check for values
if ~isvector(val)
error('Values must be a vector');
end
%Check for threshold
if ~isscalar(thresh)
error('Threshold must be a scalar');
end
%Convert the input to a row vector
in = reshape(in,1,[]);
%Lazy way of preallocating output
out = 0*in;
%Check for values present in the input
idx=ismember(in,val);
%Starting index of a cluster
ij1=strfind([0 idx 0],[0 1]);
%Ending index of a cluster
ij2=strfind([0 idx 0],[1 0])-1;
%Cluster with members more than a threshold
temp=find((ij2-ij1+1)>=thresh);
%Set the corresponding output as 1
for k=1:numel(temp)
out(ij1(temp(k)):ij2(temp(k)))=1;
end
end
  7 comentarios
Dyuman Joshi
Dyuman Joshi el 10 de Oct. de 2023
@Enzo, please check out the correction and let me know if this works for you or not.
Enzo
Enzo el 10 de Oct. de 2023
hi @Dyuman Joshi do not worry. As soon I checked it, i will send you a feedback and accept your answer

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Language Fundamentals en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by