Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Deleting value in a matrix based on the row in another matrix

Subject: Deleting value in a matrix based on the row in another matrix

From: CNN

Date: 9 May, 2010 23:09:04

Message: 1 of 4

Good day,
I wrote the code below to open a series of files, find the standard deviation of each column in the file (3) and and add each standard deviation to a new matrix. It's not perfect, I know. The problem is that some files in the series do not exist or have different names. These do not matter as I am allowed to safely skip some files. But I need to delete those values form the matrix [t] based on the matrix [point] as [point] will have all zeros rows for the missing files.

I'm just starting out in matlab and I wrote this code based on ideas from this forum. I know the problem is with this line of code
 t(:,point == 0,2) = [];
as the other work fine when I delete it. Any suggestions on the correct code will be appreciated.
Thanks
P.S I know what the code "point(all(point==0,2),:) = [];" does but what does "point==0,2" do?

point = zeros(1000,3);
    t = 41000:1000:379500;
    n = length(t);
        for i = 1:n;
            x = ['trail',num2str(t(i),'%08.0f'),'.dat'];
            g = dir (x);
            if ~isempty(g)
              M = dlmread(x,'',1,0);
              s = std (M,1);
              point(i,:) = s;
            end
        end
     t(:,point == 0,2) = [];
     point(all(point==0,2),:) = [];

Subject: Deleting value in a matrix based on the row in another matrix

From: Roger Stafford

Date: 10 May, 2010 04:32:03

Message: 2 of 4

"CNN " <cnln2000@yahoo.co.uk> wrote in message <hs7fag$90p$1@fred.mathworks.com>...
> Good day,
> I wrote the code below to open a series of files, find the standard deviation of each column in the file (3) and and add each standard deviation to a new matrix. It's not perfect, I know. The problem is that some files in the series do not exist or have different names. These do not matter as I am allowed to safely skip some files. But I need to delete those values form the matrix [t] based on the matrix [point] as [point] will have all zeros rows for the missing files.
>
> I'm just starting out in matlab and I wrote this code based on ideas from this forum. I know the problem is with this line of code
> t(:,point == 0,2) = [];
> as the other work fine when I delete it. Any suggestions on the correct code will be appreciated.
> Thanks
> P.S I know what the code "point(all(point==0,2),:) = [];" does but what does "point==0,2" do?
>
> point = zeros(1000,3);
> t = 41000:1000:379500;
> n = length(t);
> for i = 1:n;
> x = ['trail',num2str(t(i),'%08.0f'),'.dat'];
> g = dir (x);
> if ~isempty(g)
> M = dlmread(x,'',1,0);
> s = std (M,1);
> point(i,:) = s;
> end
> end
> t(:,point == 0,2) = [];
> point(all(point==0,2),:) = [];
- - - - - - - - -
  I don't know what t(:,point == 0,2) = []; does either. It would seem to imply that 't' is three-dimensional. It ought to be the same as for the next line, namely

 t(:,all(point == 0,2)) = [];

  Actually since t is a vector, there is no need for the first colon. It can be simply

 t(all(point == 0,2)) = [];

The same is not true for 'point' since it is truly two-dimensional.

Roger Stafford

Subject: Deleting value in a matrix based on the row in another matrix

From: CNN

Date: 10 May, 2010 08:36:05

Message: 3 of 4

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <hs8283$936$1@fred.mathworks.com>...
> "CNN " <cnln2000@yahoo.co.uk> wrote in message <hs7fag$90p$1@fred.mathworks.com>...
> > Good day,
> > I wrote the code below to open a series of files, find the standard deviation of each column in the file (3) and and add each standard deviation to a new matrix. It's not perfect, I know. The problem is that some files in the series do not exist or have different names. These do not matter as I am allowed to safely skip some files. But I need to delete those values form the matrix [t] based on the matrix [point] as [point] will have all zeros rows for the missing files.
> >
> > I'm just starting out in matlab and I wrote this code based on ideas from this forum. I know the problem is with this line of code
> > t(:,point == 0,2) = [];
> > as the other work fine when I delete it. Any suggestions on the correct code will be appreciated.
> > Thanks
> > P.S I know what the code "point(all(point==0,2),:) = [];" does but what does "point==0,2" do?
> >
> > point = zeros(1000,3);
> > t = 41000:1000:379500;
> > n = length(t);
> > for i = 1:n;
> > x = ['trail',num2str(t(i),'%08.0f'),'.dat'];
> > g = dir (x);
> > if ~isempty(g)
> > M = dlmread(x,'',1,0);
> > s = std (M,1);
> > point(i,:) = s;
> > end
> > end
> > t(:,point == 0,2) = [];
> > point(all(point==0,2),:) = [];
> - - - - - - - - -
> I don't know what t(:,point == 0,2) = []; does either. It would seem to imply that 't' is three-dimensional. It ought to be the same as for the next line, namely
>
> t(:,all(point == 0,2)) = [];
>
> Actually since t is a vector, there is no need for the first colon. It can be simply
>
> t(all(point == 0,2)) = [];
>
> The same is not true for 'point' since it is truly two-dimensional.
>
> Roger Stafford

Hi,

I tried your suggestion and received the error message
"Index of element to remove exceeds matrix dimensions.
Error in ==> disper36500_500 at 15
     t(all(point==0,2)) = [];"
so I added the line
"point(n+1:end,:) = [];"
before your code and it worked. Thank you so very much.

I still want to know want the code "point==0,2" does. I've read up on the "all" function but I still don't get that bit. Thanks once again.

Subject: Deleting value in a matrix based on the row in another matrix

From: Steven Lord

Date: 10 May, 2010 13:57:00

Message: 4 of 4


"CNN " <cnln2000@yahoo.co.uk> wrote in message
news:hs8ghl$22b$1@fred.mathworks.com...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in
> message <hs8283$936$1@fred.mathworks.com>...
>> "CNN " <cnln2000@yahoo.co.uk> wrote in message
>> <hs7fag$90p$1@fred.mathworks.com>...

*snip*

> I still want to know want the code "point==0,2" does. I've read up on the
> "all" function but I still don't get that bit. Thanks once again.

They're two separate expressions. The ALL function accepts two input
arguments.

" ALL(X,DIM) works down the dimension DIM. For example, ALL(X,1)
    works down the first dimension (the rows) of X."

If point is an m-by-n matrix, this will return an m-by-1 matrix with element
k being 1 (true) if all the elements in the kth row of point==0 are true
(i.e. if all the elements in that row of point are equal to 0.)

x = rand(4, 6) > 0.9
A = all(x==0, 2)

Run this a couple of times until you receive an x with an all-zero row and
you should see the corresponding element of A returned as 1.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us