How to extract particular row values from cell arrays with varying cell lengths?

Dear Matlab users, I am new to Matlab and especially handling cell arrays. I need assistance! I hope I find a solution.
I have a struct array, in that there are 2 cell arrays (Signal and Label), each of size 1*117 cell and each cell has different dimensions. So, label are from numbered from 1 to 4, and they are always in a sequence like 4,4,4,1,1,1,2,2,2,3,3,4,4,4,4,1.... so on. When label starts with 1 and ends 4 it is taken as 1 fragment/segment. It can have any number of 1,2,3,4 in them.So I want to extract first fragment from each cell (all 117) such that it comes with signal and label in one struct or 2 different cell arrays so that I can use it for training and further classification.
How would you extract it? Thank you in advance :)
here are images for the reference: total_states are labels numbered from 1 to 4, total_state_test_recordings are signal values for each label.
Also, note. dimensions for labels and signal for each cell is same.
1*117 cell of label values (total_states)
first cell of label
first cell of signal

9 commentaires

We can do nothing with images and the description/storage is complicated -- attach a (smallish) example dataset and provide what is the expected result as well.
Sorry for the confusion. Here, is a image which might clarify.
Also, here is the link to the dataset file : MatFile
Use paperclip icon and attach in the forum, not w/ external link
As the file size greater than 5 MB (around 21 MB), I am not able to attach it here in the forum, hence I had to use external link :/ It could not be compressed. Is there any other way?
  • File:myfile044totalstates.zip size exceeds 5 MB. Try compressing the image file in an archive format; for example, a zip file.
We only need a representative small sample, not the whole thing...save only a half-dozen or so array elements for demo purposes...
Sorry for the inconvience. Here is a sample file consisting of signal and label with only 3 cells with 20 values each. I hope this works! Thank you for your responses!
dpb
dpb le 6 Sep 2022
Modifié(e) : dpb le 6 Sep 2022
There's nothing changing in any of those arrays -- we don't need megabytes of data and the vectors don't have to be long, but they do have to be representative of the problem. These clearly aren't.
However, it dawns on me this appears to be a very similar searching problem as one I had in another application -- I wrote a set of helper functions there to deal with locating and processing data between two indexing rows that ended up working nicely. It, however, had sections identified by a same string with a keyword; I'll have to dig those up and look at how that would match up with the IDs you've got.
That will NOT be tonight; have other commitments to deal with first...that will give you time to put together a representative dataset.
I sincerely appreciate the time and effort you put into this. I've included a small dataset in the previous comment that could serve as a sample of the bigger dataset; is this what you were looking for?

Connectez-vous pour commenter.

 Réponse acceptée

N = numel(x.total_states);
first_fragment_labels = cell(1,N);
first_fragment_signal = cell(1,N);
for ii = 1:N
idx1 = find(x.total_states{ii} == 1, 1);
if isempty(idx1)
continue
end
idx2 = find(diff(x.total_states{ii}(idx1:end)) == -3, 1) + idx1 - 1;
if isempty(idx2)
continue
end
first_fragment_labels{ii} = x.total_states{ii}(idx1:idx2);
first_fragment_signal{ii} = x.total_test_recording{ii}(idx1:idx2);
end

Plus de réponses (0)

Catégories

Produits

Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by