How do I cut the columns in a cell of a cell array according to values in a numerical array?
6 次查看(过去 30 天)
显示 更早的评论
Hi,
I have the following problem at hand:
I have a cell array called alldata with 24 cells. The first column in each cell of alldata contains seconds as single numbers. The second column contains measurment points.
I am trying to loop through all cells in alldata and shorten the columns in each cell at the beginning and at the end a little (by deleting the first few and the last few rows) (but not for every column).
To do this, I also have a numerical array called beginning_end with 24 rows relating to each of the 24 cells in alldata. The first column in beginning_end contains cut-off points (seconds in numbers) where I want to delete the first few seconds of a column in a cell and the second column in beginning_end contains cut-off points where I want to delete the last few seconds of a column in a cell.
If there is a NaN then I dont need to cut anything (from the beginning or end of a column).
To do this I would need to (find and) match the seconds in the rows of beginning_end with the seconds in the rows of the cells of alldata to get the row numbers and know where to cut correct?
How would such code look?
(I hope this is coherent enough to understand)
Thanks!
0 个评论
回答(1 个)
Fifteen12
2022-12-16
编辑:Fifteen12
2022-12-16
I've made this a little long in order to improve legibility. Hope this works!
% Make sample data
foo = zeros(100, 2);
bar = zeros(100, 2);
for i = 2:100
foo(i, 1) = foo(i-1) + rand;
bar(i, 1) = bar(i-1) + rand;
foo(i, 2) = i;
bar(i, 2) = i;
end
alldata = {foo, bar};
beginning_end = [5.2, 20.1; 2.0, 25.5];
% Iterate through and filter
for i = 1:length(alldata)
above_min = alldata{i}(:, 1) > beginning_end(i, 1);
below_max = alldata{i}(:, 1) < beginning_end(i, 2);
valid_rows = above_min & below_max;
to_keep = alldata{i}([valid_rows, valid_rows]);
to_keep = reshape(to_keep, [height(to_keep)/2, 2]); %Reshape from logical output
alldata{i} = to_keep;
end
% Check that the data was filtered (this is just for error checking)
for i = 1:length(alldata)
disp(sprintf('Data array %d, Height=%d', i, height(alldata{i})))
disp(alldata{i}(1:2, :))
disp(sprintf('\t ...'))
disp(alldata{i}(end-1:end, :))
end
3 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!