Find Empty Fields in Matrix and Delete them

Hi All,
I am trying to create a FOR LOOP that deletes all the empty field in my matrix!
I have some rows that are full of data and some that are empty and I would like to learn the formula to do such!
I want to learn this please!
can someone demonstrate an example, Please? I am uncertain of the terminology to express!
Thank you in advance!

6 Comments

matrix rows are empty? How is your data? Can you post a sample of data?
Here i a sample,
7 columns and 150 rows
The Data is empty at the top and I want to remove the empty fields!
Oh are you looking to delete specific rows or columns
or Individuls Elements (Is replace by NaN).. , As per you data you can't do that, just to thrown "[ ]" only Matrix existance ?
To get the terminology straight: you have a cell array.
Arrays in Matlab must always be rectangular. Which rows of your cell array do you want to remove? All lines where at least one cell is empty? Or only those rows where there are only empty cells?
I am trying to delete the rows that are [] empty or even better write a for loop that selects all of the data from the rows that has data within them!
Hi Rik
All lines where at least one cell is empty, Please guide me? and thank you for the confirmation on the row arrays!!

Sign in to comment.

Answers (1)

Rik
Rik on 21 Apr 2020
Edited: Rik on 21 Apr 2020
%generate fake data
data=cell(30,10);
for n=1:numel(data)
if rand>0.2
data{n}=rand(1,5);
end
end
%find all cells that don't contain data
L=cellfun('isempty',data);%faster than L=cellfun(@isempty,data);
%remove rows with any empty cell
emptyrows=any(L,2);
data(emptyrows,:)=[];
You can also use L to loop through the elements that have data in them (just invert it with the ~ operator).

8 Comments

Sorry Rik, I don;t get it!
This is my code!
%% Specify The Location Of The Image
countNumSdataRec= size(SData2Table,1);% contunting the number of rows
for i = 1:countNumSdataRec
FrameNames{i,1} = fullfile(Violence,'Shooting',sprintf('%i.jpg',SDataFrames(i)));% creating an image name
end
SDataTable = [FrameNames,SData2Table]; %Linking the Frame Name With Beating Data
%% Checking For Empty Fields Within SDataTable
countNumEmptyFields=cell(SDataTable);
for n=1:numel(countNumEmptyFields)
if rand>0.2
countNumEmptyFields{n}=rand(:,:);
end
end
%find all cells that don't contain data
L=cellfun('isempty',countNumEmptyFields);%faster than L=cellfun(@isempty,data);
%remove rows with any empty cell
emptyrows=any(L,:);
data(emptyrows,:)=[];
I used some code to generate some data so the code works. You already have data, so you only need to replace the variable name.
Why did you change any(L,2) to any(L,:)? Have you read the documentation for the any function?
%find all cells that don't contain data
L=cellfun('isempty',SData2Table);%faster than L=cellfun(@isempty,data);
%remove rows with any empty cell
emptyrows=any(L,2);
SData2Table(emptyrows,:)=[];
Yes!! I am learning and progressing thus far! I did research this and it was confusing me, so I sorted a break down of how it works and the functions that it utilises!
Can you help me to understand this doing it the for loop method, Please?
Why do you want a for-loop? Is there any step in this code you find confusing?
Just would like to see how to put this in a for loop so that I can learn and gain the knowlede of the task!
What is the 2 for here?
emptyrows=any(L,2);
If you insist, you could do this:
%replace
L=cellfun('isempty',SData2Table);
%with this
L=false(size(SData2Table));%pre-allocate result
for n=1:numel(SData2Table)
L(n)=isempty(SData2Table{n});
end
%or this
L=false(size(SData2Table));%pre-allocate result
for r=1:size(SData2Table,1)
for c=1:size(SData2Table,2)
L(r,c)=isempty(SData2Table{r,c});
end
end
I don't see how the second and third option would be easier to understand.
If you don't understand a function syntax, you should read the documentation. What part of the documentation of any don't you understand? If you've been working long enough with Matlab to have posted 61 questions you should have learned to use the documentation.
Matpar
Matpar on 21 Apr 2020
Edited: Matpar on 21 Apr 2020
Thank you for breaking down my confidence RIK!
this may come easy for you but nevertheless the 60 odd questions they were posted becasue of wanting a better method or being confused to the point of giving up!
Sorry for displying the ID10T error!! I will try the documentations next time, I think I will stop coding for the week
I didn't mean to insult you. I meant exactly what I said: I don't see how they would be easier to understand than the oneliner. And the Matlab documentation is an excellent resource, so you will be missing out massively if you don't learn to use it. Nobody is born knowing this, everyone who knows it was taught this at some point.
If you have trouble understanding the documentation I would be happy to explain it to you. I only wanted to direct you to it because if you learn how to help yourself it would save you waiting for a reply from me or someone else.

Sign in to comment.

Asked:

on 21 Apr 2020

Commented:

Rik
on 21 Apr 2020

Community Treasure Hunt

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

Start Hunting!