Finding index values for consecutive values in cell array
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Dylan George
am 19 Mär. 2019
Kommentiert: Guillaume
am 20 Mär. 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 Kommentare
Guillaume
am 20 Mär. 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
Akzeptierte Antwort
Guillaume
am 20 Mär. 2019
Bearbeitet: Guillaume
am 20 Mär. 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 Kommentare
Guillaume
am 20 Mär. 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').
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!