How to take out a range of values from matrices within a cell with a for loop?
2 次查看(过去 30 天)
显示 更早的评论
My issue is that I have uneven matrices/vectors which I have put into a cell. I need to manipulate this data starting from a specific point. Therefore, I need to cut out the first few points and the last few to get the information that I need. I know how to do this manually, but that's just a hassle so I was wondering if there was a way to make it a for loop?
My data within the cell that I have attached is anywhere from 4500x1 to 4800x1 matrices and need to basically take out all of these matrices and make a similar cell that contains the first few hundred points taken out and the last few hundred taken out to make all of the arrays in the cells be around 3400x1 lets say.
3 个评论
OCDER
2018-6-26
What about sequences that are 3400 long? Do you extract 1:3400 then, since there are no 600:4000? By the way, 600:4000 would give you 3401 elements. Did you want 601:4000?
采纳的回答
Guillaume
2018-6-26
I gave a general example in the question because if someone provides an answer I can fix those values myself
Yes, but there are many ways of cropping arrays depending on the need. So our answer may not be adapted at all to what you really want.
However, there are 3 arrays in the cell that are only 1000 long so I would not be using them
yourcellarray(cellfun(@numel, yourcellarray) < 3400) = []; %remove cells which contain arrays of less than 3400 elements
Assuming you want to crop the remaining arrays to 3400 elements, removing elements from the start:
newcellarray = cellfun(@(arr) arr(end-3399:end), yourcellarray, 'UniformOutput', false);
2 个评论
OCDER
2018-6-26
A variation of @Guillaume's answer if you want to preserve the original data cell:
% Y is your cell array
N = cellfun(@(x) x(end-3399:end), Y(cellfun(@numel, Y)>=3400), 'un', 0);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!