Delete specific rows from a cell array and create a new cell array

1 view (last 30 days)
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.

Answers (1)

dpb
dpb on 13 Dec 2019
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 Comments
dpb
dpb on 14 Dec 2019
"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
Ifthekhar Ahammad on 28 Dec 2019
@ dpb, Thank you very much for your advice, after the holidays I tried to followup the code and used a different approach, in a way the same thing that you did but with a different approach:
for i=2:size(T,1)
if ~(T(i-1,8)<T(i,8) && T(i,8)<T(i+1,8))
fprintf('%s%i%s%i%s','Problem in line: ', i,' Value is: ',T(i,8),newline);
T(i,:) = [];
end
if i> size(T,1)-1
break;
end
end
That portion worked just fine. but now I am stuck with a different problem. Even after deleting those points I am left with few more points that do not allow me to plot everything property. Just to recap, column 8 is the value of "time" in milliseconds (x-axis) and all the other columns are the corresponding values of 7 different sensors (y-axis). Anyway, after using the previously mentioned code, I only tried to plot the column 8. with this command:
figure(3);
% x2=plot(C1{8}, '*');
plot(T(:,8), '*')
ylim([-1 80000000]);
This was the result:
Untitled.png
So, the next challenge is that I want to keep all the data points that will create that solid blue (red arrow) line and want to delete all the data points from the matrix that is above or below that line. My instinct tells me that I should figure out the equation (y=mx) of that line and use that equation as the condition of the "if loop" that I mentioned earlier, to remove those unwanted data points.
I can manually select two points from the graph and figure out the equation of that line. But is there a function in MATLAB that will automatically detect the equation of that line?
and
Is there any command in MATLAB that will automatically delete all the data points above or below that line?
Again thank you very much for all the help.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!