Finding index values for consecutive values in cell array

2 次查看(过去 30 天)
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 个评论
Guillaume
Guillaume 2019-3-20
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 2019-3-20
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,

请先登录,再进行评论。

采纳的回答

Guillaume
Guillaume 2019-3-20
编辑:Guillaume 2019-3-20
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 个评论
Dylan George
Dylan George 2019-3-20
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 2019-3-20
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').

请先登录,再进行评论。

更多回答(1 个)

Dylan George
Dylan George 2019-3-20
Tried again and worked! Thanks Guillaume!!

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by