Here's the problem, if I understand it correctly:
a =
1 2 3 4 5
2 3 4 -99 6
3 4 5 6 7
4 -99 6 7 8
5 6 7 8 9
How to remove all the rows that contains the number -99, in this case, the row #2 and #4?
And here's the code I wrote to do this job:
[rows col]=size(a); %count the number of rows and columns of matrix a
j=0; %initialize a counter;
for i=1: rows
if sum(a(i,:)~=-99)==col; %This determines if the row has -99 or not, if not, do nothing
else %shift all of the rest of the rows up one row
for m=i: rows-1;
a(m,:)=a(m+1,:);
end
j=j+1; %count how many rows has -99, which equals how many times the rows has been shifted up
end
end
b=a((1:rows-j),:); %get rid of the last j rows
The result is:
b =
1 2 3 4 5
3 4 5 6 7
5 6 7 8 9
The result is:
b =
1 2 3 4 5
3 4 5 6 7
5 6 7 8 9
I hope this can work for you, Winifred. Thanks for your comments!
a better solution would be:
ReplyDeletea = [1 2 3 4 5;2 3 4 -99 6;3 4 5 6 7;4 -99 6 7 8;5 6 7 8 9];
%Remove any row with -99
a(any(a==-99,2),:) = []
a = [1 2 3 4 5;2 3 4 -99 6;3 4 5 6 7;4 -99 6 7 8;5 6 7 8 9];
%Remove any column with -99
a(:, any(a==-99,1)) = []
I'm a new bie and kindly explain me this doubt,
Deletea(any(a==-99,2),:) = []
what does -99,2 denotes in this line?
-99 is a made up number, in this case, I want to remove the row (or the column) if that row (or column)contains -99. It could be any other number.
DeleteThe 2 and 1 in the 'any' function is a parameter that tells the function to look either into rows or into columns. Here's the reference for 'any' function:
http://www.mathworks.com/help/techdoc/ref/any.html
Thanks for your post.
Hey cguitar,
DeleteHope you can help me on this. I have a big matrix say,
MAT[1 1 4
1 1.5 7
2 1.2 6
2 1.6 8]
How to remove the rows that contains values greater than 1.5 in second column.
Help me out pls. Thanks
You can use Excel to do this job easily.
DeletePlease check the number filter function at this link:
http://office.microsoft.com/en-us/excel-help/filter-data-in-a-range-or-table-HP010073941.aspx
That's really nice! Thank you very much, Rafael!
ReplyDeleteHi friends, I am newcomer to Matlab. I have got a big 720x19 matrix in the form of a .mat file. This is the data of wind speed for one full month. I want to plot this. I have to remove the elements when the value is 999.
ReplyDeleteI loaded the file as:
a=load('sep11hznb.mat')
Now how should I proceed to achieve my goal?
Hi Kutty,
ReplyDeleteWhat do you want to replace the 999 with? If you just want to remove the 999 and leave a 'hole' there, you can use this comand:
a(a==999)=NaN;
If you want to replace the 999 with another number, say 0, this will do it:
a(a==999)=0;
Good luck!
Hello cguitar,
DeleteWhat if you want to replace a text value like NULL with a value of 0 or any other number value, for example? After reading my matrix in, I tried the same command
block2(block2==NULL) = 0;
but it didn't work.
Thanks for your help
Hello Muneer,
DeletePlease try this and let me know if it works.
block2( strcmp(block2, 'NULL') )=0;
:)
Thank you very much cguitar...Appreciate it..!
ReplyDeleteI have 10000 data sets of (x,y) for which i need to fit a curve. Once I could fit a curve, i need to get points on the curve with an uniform interval. Then I would proceed to patch..and then to stl for my ultimate objective. Right now i'm struck with the curve fitting. Kindly help me out.
ReplyDeleteLink for the description: https://docs.google.com/open?id=0BxIgxUb0m51-ZDA2ZWU0NzItM2JkZS00MWI4LTg0ZTMtNTI0ZjQzMzIxMzU3
Sorry I can't help on this one, because I am not familiar with the curve fitting tools and I don't get your question.
DeleteCguitar,
ReplyDeleteCan you help me with the last question :)
I was looking for a matlab option to do the following.
ReplyDeleteI have a big matrix say,
MAT[1 1 4
1 1.5 7
2 1.2 6
2 1.6 8]
How to remove the rows that contains values greater than 1.5 in second column.
Help me out pls. Thanks
You can use Excel to do this job easily.
ReplyDeletePlease check the number filter function at this link:
http://office.microsoft.com/en-us/excel-help/filter-data-in-a-range-or-table-HP010073941.aspx
how can i count the number of deleted rows ? and make them answer appeaar in the command window
ReplyDeletehow can I divide bin from a big matrix of image
ReplyDeleteI don't quite understand your question. If you can provide more description of your problem or give a sample dataset, that would be helpful.
Deletehow can i select some points(numbers) in the matrix file
ReplyDeletesay if you have a matrix M. you can use
Deletea=M(i,j)
to assign the element in ith row jth column to a.
I hope this answers your question :)
And what if you have words, let's say 'hello', several times in yoir matrix and you want to change this for zeros?
ReplyDeleteHi Lacor,
DeleteConverting string ('hello') to numbers is trickier. It also depends on if you stored your data in cell array or structure or dataset.
If you can leave more information about your data, I'd be glad to look into them.
Removing rows containing specific criteria is a vital data cleaning operation. Fun Game Ideas In programming or data analysis, it entails identifying and eliminating rows that meet certain conditions.
ReplyDelete