Finding index values for consecutive values in cell array

2 visualizaciones (últimos 30 días)
Dylan George
Dylan George el 19 de Mzo. de 2019
Comentada: Guillaume el 20 de Mzo. de 2019
Hi all,
I have a 4d cell array: 105 x 3 x3 x 2000 . I would like to find the average number of consecutive values above a threshold value in the 2000 frames throughout 105cells
105 cells look abit like this: (made up numbers)
val(:,:,1) =
1 2 1
6 3 2
1 2 2
to -> val(:,:,2000) =
1 2 1
1 3 2
1 2 1
I would like to find the maximum value in each 2000 cells of my 105 cells ( so 6 for val(:,:,1) and 3 for val(:,:,2000) in this case for cell 1 ). I want set a threshold of a number say 6 ( so only finds val(:,:,1) in this case). Then I want to know how many frames it was above this threshold value for.. so maybe val(:,:,1) to val(:,:,7) which would be 7 frames.
An average of the number of these frames throughout the whole cell array would be ideal.
I am not so good at coding so I need all the help I can get. Thank you in advance!
  2 comentarios
Guillaume
Guillaume el 20 de Mzo. de 2019
I'm confused by your explanations. You say you have 4D array but all your explanation use 3D indexing and seem to ignore the 1st dimension. (So it looks like your first matrix is squeeze(val(1, :, :, 1)) and the 2nd one is squeeze(val(1, :, :, 2000)) maybe.
You also say that you have a cell array, yet your examples use matrices.
It's also unclear whether or not your threshold is an upper or lower threshold (and whether equal values should be kept). I think you want to keep values >= threshold.
What is an average of the number of these frames?
I understood of what the final result should be.
It probably would be best if you gave a complete example of input (using a smaller matrix or cell array) and the desired output for that example, using valid matlab syntax (so there's no ambiguity on the type of the arrays). Either that or attach a mat file to your question
Dylan George
Dylan George el 20 de Mzo. de 2019
Hi Guillaume, thanks for taking the time to answer.
I've attatched a .mat file of the cell array now so it should be a bit clearer than my attempted explanation.
The threshold I want to find is all values above 19. If they are above 19 then I basically want to find how long for, eg. so if there are max values of the 3x3 matrix equal 15, 20, 21, 22, 10, 30, 12, 13 in the 2000 frames. Then there are 3 values together (20, 21, 22) and then 1 (30) consecutively above 19. So the mean would be 3+1/2 = 2 for that cell. I would like to do that for all 105 cells.
Thanks,

Iniciar sesión para comentar.

Respuesta aceptada

Guillaume
Guillaume el 20 de Mzo. de 2019
Editada: Guillaume el 20 de Mzo. de 2019
Right, so your data is a 105x1 vector cell array of 3x3x2000 matrices.
First, a function to get the average length of the sequences in a vector:
function meanlength = averagesequencelength(sequence)
%sequence: a logical vector of any shape. The code does not check that the input is indeed a vector
transitions = find(diff([false; sequence(:); false])); %find location transitions between false;true and true;false. Padding to ensure that the sequence always start with a false;true transition and ends on a true;false transition
seqlengths = transitions(2:2:end) - transitions(1:2:end); %we're guaranteed that the number of transitions is even due to the padding
meanlength = mean(seqlengths);
if isnan(meanlength) %will happen if there is no sequence at all
meanlength = 0;
end
end
Then to apply to your cell array:
threshold = 19;
result = cellfun(@(m) averagesequencelength(max(m, [], [1 2]) >= threshold), SNRcellarray)
Note that the above use the new dimension syntax for max introduced in R2018b. In earlier versions:
result = cellfun(@(m) averagesequencelength(max(max(m, [], 1), [], 2) >= threshold), SNRcellarray)
  3 comentarios
Dylan George
Dylan George el 20 de Mzo. de 2019
Also tried an alternative method:
snrthreshold = 19;
snrrows = cellfun(@(m) find(max(m(:,:,:)) > snrthreshold), SNRcellarray, 'UniformOutput', false);
e= cellfun(@(a)~isempty(a)&a>0,snrrows,'UniformOutput', false);
snrrows = vertcat(snrrows{:})
This gives me snr rows (attatched) a double of all the frames. Still trying to find sequence length of them though.
Guillaume
Guillaume el 20 de Mzo. de 2019
I made a typo in the function code (forgot the 's' at the end of seqlengths when it's used in the mean call. However, that would have results in an 'Undefined function or variable seqlength' error, not a 'not enough input arguments' error. I don't see how you could get that error.
If've fixed the code (added the 's').

Iniciar sesión para comentar.

Más respuestas (1)

Dylan George
Dylan George el 20 de Mzo. de 2019
Tried again and worked! Thanks Guillaume!!

Categorías

Más información sobre Symbolic Math Toolbox en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by