Find Empty Fields in Matrix and Delete them
Ältere Kommentare anzeigen
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 Kommentare
KSSV
am 21 Apr. 2020
matrix rows are empty? How is your data? Can you post a sample of data?
KALYAN ACHARJYA
am 21 Apr. 2020
Bearbeitet: KALYAN ACHARJYA
am 21 Apr. 2020
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 ?
Rik
am 21 Apr. 2020
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?
Matpar
am 21 Apr. 2020
Matpar
am 21 Apr. 2020
Antworten (1)
%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 Kommentare
Matpar
am 21 Apr. 2020
Rik
am 21 Apr. 2020
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,:)=[];
Matpar
am 21 Apr. 2020
Rik
am 21 Apr. 2020
Why do you want a for-loop? Is there any step in this code you find confusing?
Matpar
am 21 Apr. 2020
Rik
am 21 Apr. 2020
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.
Rik
am 21 Apr. 2020
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.
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
