Finding index values for consecutive values in cell array

4 ビュー (過去 30 日間)
Dylan George
Dylan George 2019 年 3 月 19 日
コメント済み: Guillaume 2019 年 3 月 20 日
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 Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by