Delete specific rows from a cell array and create a new cell array
显示 更早的评论
Hello Everyone,
I have created a 1x8 cell array from a text file. I want to create a new cell array that will only have a particular feature. If my cell array is "S" then I want to use a condition on S{8}. if any element (n) of S{8} is greater than its next element (n+1) I want to delete the entire row and after deleting all the unwanted rows, I want to create a new cell array with the remaining elements.
For example:
if I have these elements in my cell array "S"
0,0,1,0,0,0,0,10761
0,0,1,0,0,0,0,11067
0,0,1,0,0,0,0,3036687367
0,0,1,0,0,0,0,11271
.
.
0,0,0,0,0,0,0,28509
0,0,0,0,0,0,0,1656057693
0,0,0,0,0,0,0,28713
0,0,0,0,0,0,0,28815
I want to create a new cell array "T" that will only have these elements:
0,0,1,0,0,0,0,10761
0,0,1,0,0,0,0,11067
0,0,1,0,0,0,0,11271
.
.
0,0,0,0,0,0,0,28509
0,0,0,0,0,0,0,28713
0,0,0,0,0,0,0,28815
Any Suggestions?
Thanks.
回答(1 个)
dpb
2019-12-13
Place where converting the cell array to regular double array makes sense it would seem...there's nothing in the above data that isn't numeric so just use the native double array.
S=cell2mat(S); % convert to double array
ix=[false; (diff(S(:,8))>0)]; % locations of +-ive difference
S(ix,:)=[]; % remove those rows
5 个评论
Ifthekhar Ahammad
2019-12-14
编辑:dpb
2019-12-14
dpb
2019-12-14
" ... later on, I want to plot each of the 1 to 7 (S{1} to S{7}) columns against column 8 (S{8}) using something like this command:"
Just use
plot(S(:,1),S(:,7))
instead once have the double array. One colon and a comma are no more characters than two curlies. :)
fid=fopen('OVRNITV1.txt','r');
if fid == -1
error('Error opening txt file')
end
T=cell2mat(textscan(fid, '%d %d %d %d %d %d %d %d', 'Delimiter', ','));
fid=fclose(fid);
ix=[false; (diff(T(:,8))>0)]; % locations of +-ive difference
T(ix,:)=[]; % remove those rows
removing superfluous code...convert to array when read; once you have the array there's no need for the cell array any longer.
dpb
2019-12-14
"Here you can see that the code has also skipped some data points (in S{1,8} Row :42 is now Row: 3 in T matrix) and many more."
No, we can't see anything of the sort. What does "the code has skipped some data points" mean? It deleted everything for which diff(S)>0 as per the original specification--that there are duplicated values and therefore a set of points for which the difference is now zero isn't anything that was given as a requirement.
Would have to see all the original data to know; perhaps the condition should be
ix=[false; (diff(T(:,8))>=0)];
but can't tell without a definitive statement of what is the desired result; some heuristic that may work for a given dataset isn't a specification.
Ifthekhar Ahammad
2019-12-28
类别
在 帮助中心 和 File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

