Thread Subject: Problem using a for loop to delete rows and columns from a matrix.

Subject: Problem using a for loop to delete rows and columns from a matrix.

From: Mark Hard

Date: 22 Oct, 2009 11:06:03

Message: 1 of 16

Hi,

Basically I have an NxN matrix, and I have a vector of length M.

What I need to do is go though the values contained in the vector and say I find a value of "i", I need to go to the matrix and delete the row i and the column i from the matrix, then repeat the process for every value in the vector.

I tested it using thde code below using examples of values for the matrix and vector,

k =

     1 2 3 4 5 6 7 8
     9 10 11 12 13 14 15 16
    17 18 19 20 21 22 23 24
    25 26 27 28 29 30 31 32
    33 34 35 36 37 38 39 40
    41 42 43 44 45 46 47 48
    49 50 51 52 53 54 55 56
    57 58 59 60 61 62 63 64

n = 8; % n = number of degrees of freedom in whole system. (could be a very high number)
DOFc = [2 3]; %the degrees of freedom which are constained will be put in this vector. (could be a very long vector)
for ii = 1:n
    for jj = 1:length(DOFc)
        if ii == DOFc(jj)
            k(ii,:) = []; %removes the i'th row
            k(:,ii) = []; %removes the i'th column
        else
        end
    end
end

k =

     1 3 5 6 7 8
    17 19 21 22 23 24
    33 35 37 38 39 40
    41 43 45 46 47 48
    49 51 53 54 55 56
    57 59 61 62 63 64

  
I needed to delete the 2nd and 3rd rows and vectors of k but instead it has deleted the 2nd and 4th. Can anyone see what's wrong?

Any input would be greatly appreciated.

Mark.

Subject: Problem using a for loop to delete rows and columns from a matrix.

From: nor ki

Date: 22 Oct, 2009 12:10:24

Message: 2 of 16

"Mark Hard" <GerryTheLeper@hotmail.com> wrote in message <hbpear$aea$1@fred.mathworks.com>...
> Hi,
>
> Basically I have an NxN matrix, and I have a vector of length M.
>
> What I need to do is go though the values contained in the vector and say I find a value of "i", I need to go to the matrix and delete the row i and the column i from the matrix, then repeat the process for every value in the vector.
>
> I tested it using thde code below using examples of values for the matrix and vector,
>
> k =
>
> 1 2 3 4 5 6 7 8
> 9 10 11 12 13 14 15 16
> 17 18 19 20 21 22 23 24
> 25 26 27 28 29 30 31 32
> 33 34 35 36 37 38 39 40
> 41 42 43 44 45 46 47 48
> 49 50 51 52 53 54 55 56
> 57 58 59 60 61 62 63 64
>
> n = 8; % n = number of degrees of freedom in whole system. (could be a very high number)
> DOFc = [2 3]; %the degrees of freedom which are constained will be put in this vector. (could be a very long vector)
> for ii = 1:n
> for jj = 1:length(DOFc)
> if ii == DOFc(jj)
> k(ii,:) = []; %removes the i'th row
> k(:,ii) = []; %removes the i'th column
> else
> end
> end
> end
>
> k =
>
> 1 3 5 6 7 8
> 17 19 21 22 23 24
> 33 35 37 38 39 40
> 41 43 45 46 47 48
> 49 51 53 54 55 56
> 57 59 61 62 63 64
>
>
> I needed to delete the 2nd and 3rd rows and vectors of k but instead it has deleted the 2nd and 4th. Can anyone see what's wrong?
>
> Any input would be greatly appreciated.
>
> Mark.

Hi Mark,
 
a maybe faster solution:

k(DOFc,:) = [];
k(:,DOFc) = [];

I have to admitt: i did notreally read your code but my fast suspicion is:

as you delete row 2, row 3 gets on its position and the row4 is then the new row 3, so it gets deleted.

you could overcome this by running the loop in reverse direction

hth
kinor

Subject: Problem using a for loop to delete rows and columns from a matrix.

From: Steven Lord

Date: 22 Oct, 2009 13:13:57

Message: 3 of 16


"Mark Hard" <GerryTheLeper@hotmail.com> wrote in message
news:hbpear$aea$1@fred.mathworks.com...
> Hi,
>
> Basically I have an NxN matrix, and I have a vector of length M.
>
> What I need to do is go though the values contained in the vector and say
> I find a value of "i", I need to go to the matrix and delete the row i and
> the column i from the matrix, then repeat the process for every value in
> the vector.
>
> I tested it using thde code below using examples of values for the matrix
> and vector,
>
> k =
>
> 1 2 3 4 5 6 7 8
> 9 10 11 12 13 14 15 16
> 17 18 19 20 21 22 23 24
> 25 26 27 28 29 30 31 32
> 33 34 35 36 37 38 39 40
> 41 42 43 44 45 46 47 48
> 49 50 51 52 53 54 55 56
> 57 58 59 60 61 62 63 64
>
> n = 8; % n = number of degrees of freedom in whole system. (could be a
> very high number)
> DOFc = [2 3]; %the degrees of freedom which are constained will be put in
> this vector. (could be a very long vector)
> for ii = 1:n
> for jj = 1:length(DOFc)
> if ii == DOFc(jj)
> k(ii,:) = []; %removes the i'th row
> k(:,ii) = []; %removes the i'th column
> else
> end
> end
> end
>
> k =
>
> 1 3 5 6 7 8
> 17 19 21 22 23 24
> 33 35 37 38 39 40
> 41 43 45 46 47 48
> 49 51 53 54 55 56
> 57 59 61 62 63 64
>
>
> I needed to delete the 2nd and 3rd rows and vectors of k but instead it
> has deleted the 2nd and 4th. Can anyone see what's wrong?

What happens after the first iteration through the inner loop? You've
deleted the 2nd row and column of your original k. That means what was the
3rd row in your original k is now the 2nd row of your new k. Then when you
go through the loop again, you delete the 3rd row from your new k which, as
you've noticed, is the 4th row of your original k.

Use nor ki's solution, which doesn't involve any loops:

k(DOFc, :) = [];
k(:, DOFc) = [];

This deletes the 2nd and 3rd rows of your original k all at once, so the
matrix only "collapses" after the rows have been deleted, not during the
deletion process.

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

Subject: Problem using a for loop to delete rows and columns from a matrix.

From: Mark Hard

Date: 28 Oct, 2009 14:10:19

Message: 4 of 16

"Steven Lord" <slord@mathworks.com> wrote in message <hbplpa$33o$1@fred.mathworks.com>...
>
> "Mark Hard" <GerryTheLeper@hotmail.com> wrote in message
> news:hbpear$aea$1@fred.mathworks.com...
> > Hi,
> >
> > Basically I have an NxN matrix, and I have a vector of length M.
> >
> > What I need to do is go though the values contained in the vector and say
> > I find a value of "i", I need to go to the matrix and delete the row i and
> > the column i from the matrix, then repeat the process for every value in
> > the vector.
> >
> > I tested it using thde code below using examples of values for the matrix
> > and vector,
> >
> > k =
> >
> > 1 2 3 4 5 6 7 8
> > 9 10 11 12 13 14 15 16
> > 17 18 19 20 21 22 23 24
> > 25 26 27 28 29 30 31 32
> > 33 34 35 36 37 38 39 40
> > 41 42 43 44 45 46 47 48
> > 49 50 51 52 53 54 55 56
> > 57 58 59 60 61 62 63 64
> >
> > n = 8; % n = number of degrees of freedom in whole system. (could be a
> > very high number)
> > DOFc = [2 3]; %the degrees of freedom which are constained will be put in
> > this vector. (could be a very long vector)
> > for ii = 1:n
> > for jj = 1:length(DOFc)
> > if ii == DOFc(jj)
> > k(ii,:) = []; %removes the i'th row
> > k(:,ii) = []; %removes the i'th column
> > else
> > end
> > end
> > end
> >
> > k =
> >
> > 1 3 5 6 7 8
> > 17 19 21 22 23 24
> > 33 35 37 38 39 40
> > 41 43 45 46 47 48
> > 49 51 53 54 55 56
> > 57 59 61 62 63 64
> >
> >
> > I needed to delete the 2nd and 3rd rows and vectors of k but instead it
> > has deleted the 2nd and 4th. Can anyone see what's wrong?
>
> What happens after the first iteration through the inner loop? You've
> deleted the 2nd row and column of your original k. That means what was the
> 3rd row in your original k is now the 2nd row of your new k. Then when you
> go through the loop again, you delete the 3rd row from your new k which, as
> you've noticed, is the 4th row of your original k.
>
> Use nor ki's solution, which doesn't involve any loops:
>
> k(DOFc, :) = [];
> k(:, DOFc) = [];
>
> This deletes the 2nd and 3rd rows of your original k all at once, so the
> matrix only "collapses" after the rows have been deleted, not during the
> deletion process.
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>

Hi thanks for the reply.

That worked fine. But now I have a problem which leads on from this. Using the above I can calculate a column vector which has values relating to deplacements. The column vector will have the same height (say 'm') as the k matrix size is m x m after the rows and columns have been canceled. My problem now is that the column vector needs to have a height of 'n', where n is the original size of the k matrix before the rows and columns were deleted (n x n). Basically I need to add zeroes into the column where the rows and colums were canceled out. (row 2 and 3 from the above example.

So for example at the moment the column vector looks something like this..

1
4
5
6
7
8

I need it to automatically sub in the zeros in the right places (from the first part) to look like this.

1
0
0
4
5
6
7
8

Remember that the value of n (original k matrix dimension) can be very large.

Any ideas?

Advanced thanks.

Subject: Problem using a for loop to delete rows and columns from a matrix.

From: the cyclist

Date: 28 Oct, 2009 14:54:02

Message: 5 of 16

"Mark Hard" <GerryTheLeper@hotmail.com> wrote in message <hc9jcb$45i$1@fred.mathworks.com>...
>
> I need it to automatically sub in the zeros in the right places (from the first part) to look like this.

In the original solution, you did commands of the form:

>> x(index) = [];

which replaces the indexed locations with an empty array, effectively collapsing them. Can you just put in zeros instead?

>> x(index) = 0;

In some circumstances, you need to create an array of zeros of the correct size on the right-hand side, but generally MATLAB figures that out.

the cyclist

Subject: Problem using a for loop to delete rows and columns from a matrix.

From: Mark Hard

Date: 28 Oct, 2009 15:16:02

Message: 6 of 16

Thanks for the reply.

When I use the command,

d(DOFc, :) = 0

I get a column vector that looks like

1
0
0
6
7
8

rather than,

1
0
0
4
5
6
7
8

Can this command definately be used to "push" values into the vector without replacing the old ones? or do I need another command?

Subject: Problem using a for loop to delete rows and columns from a

From: Nathan

Date: 28 Oct, 2009 16:25:35

Message: 7 of 16

On Oct 28, 8:16 am, "Mark Hard" <GerryTheLe...@hotmail.com> wrote:
> Thanks for the reply.
>
> When I use the command,
>
> d(DOFc, :) = 0
>
> I get a column vector that looks like
>
> 1
> 0
> 0
> 6
> 7
> 8
>
> rather than,
>
> 1
> 0
> 0
> 4
> 5
> 6
> 7
> 8
>
> Can this command definately be used to "push" values into the vector without replacing the old ones? or do I need another command?

Use
k(DOFc, :) = 0

rather than
k(DOFc, :) = []

Ex:

k(DOFc,:) = 0
%%%%%%%%%%%%%%%%%%%
k =
     1 2 3 4 5 6 7 8
     0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0
    25 26 27 28 29 30 31 32
    33 34 35 36 37 38 39 40
    41 42 43 44 45 46 47 48
    49 50 51 52 53 54 55 56
    57 58 59 60 61 62 63 64
%%%%%%%%%%%%%%%%%%%
k(:, DOFc) = []
%%%%%%%%%%%%%%%%%%%
k =
     1 4 5 6 7 8
     0 0 0 0 0 0
     0 0 0 0 0 0
    25 28 29 30 31 32
    33 36 37 38 39 40
    41 44 45 46 47 48
    49 52 53 54 55 56
    57 60 61 62 63 64

Get it?

-Nathan

Subject: Problem using a for loop to delete rows and columns from a

From: Mark Hard

Date: 28 Oct, 2009 16:55:17

Message: 8 of 16

Thanks for the reply.

But I can't use this method either because I need to invert the n x n matrix in order to calculate the values in my column vector. So both the rows and columns need to be deleted from it.

Subject: Problem using a for loop to delete rows and columns from a

From: Peter Brooks

Date: 28 Oct, 2009 17:14:02

Message: 9 of 16

"Mark Hard" <GerryTheLeper@hotmail.com> wrote in message <hc9t1l$jlf$1@fred.mathworks.com>...
> Thanks for the reply.
>
> But I can't use this method either because I need to invert the n x n matrix in order to calculate the values in my column vector. So both the rows and columns need to be deleted from it.

Hi Mark,

If you're wanting the column vector you displayed before, you can just do the following:

colvec = 1:size(k,1);
colvec(DOFc)=0;

Does that do the trick...?

Subject: Problem using a for loop to delete rows and columns from a

From: Mark Hard

Date: 28 Oct, 2009 17:57:04

Message: 10 of 16

Thanks for the reply.

I've been playing around with the commands that you wrote here. I'm not sure what they achieve but I think if I use them to sub in the 0 values they'll overwrite the previous value rather then bumping them down the column and increasing the height of the column vector which is what I want them to do (so that the vector will have the same amount of rows that the n x n matrix had before I crossed off the rows and vectors earlier, i.e. a "height" of n). Correct me if I'm wrong ofc. I hope I'm making sense. Any questions just ask because I may be doing something stupid.

Subject: Problem using a for loop to delete rows and columns from a

From: the cyclist

Date: 28 Oct, 2009 18:14:21

Message: 11 of 16

"Mark Hard" <GerryTheLeper@hotmail.com> wrote in message <hca0lg$an4$1@fred.mathworks.com>...
> Thanks for the reply.
>
> I've been playing around with the commands that you wrote here. I'm not sure what they achieve but I think if I use them to sub in the 0 values they'll overwrite the previous value rather then bumping them down the column and increasing the height of the column vector which is what I want them to do (so that the vector will have the same amount of rows that the n x n matrix had before I crossed off the rows and vectors earlier, i.e. a "height" of n). Correct me if I'm wrong ofc. I hope I'm making sense. Any questions just ask because I may be doing something stupid.

You seem to be doing

>> x(index) = [];

AND THEN

>> x(index) = 0;

What we are saying is that you could use

>> x(index) = 0;

INSTEAD OF

>> x(index) = [];

to replace the values with zero. If you also need the collapsed version, then maybe you need two copies of the array.

Subject: Problem using a for loop to delete rows and columns from a

From: Mark Hard

Date: 28 Oct, 2009 19:05:19

Message: 12 of 16

Thanks for another reply.

You see, in order for me to get the column vector, I need to invert the array after I have cut the rows and columns out of it and multiply it by a different column vector (m x 1) which also has values cut from it in order for the multiplication to be possible [ (m x m) x (m x 1) leaves an (m x 1) ]. This remaining m x 1 is the culumn vector I need to sub the 0 values back into the correct places in order for it to become an n x 1.

So if I use that method and I get left with something like... (as mentioned above)

k =
     1 4 5 6 7 8
     0 0 0 0 0 0
     0 0 0 0 0 0
    25 28 29 30 31 32
    33 36 37 38 39 40
    41 44 45 46 47 48
    49 52 53 54 55 56
    57 60 61 62 63 64

This will not invert and I cannot work out the column vector I am trying to sub the 0's back into now. I hope I'm making sense. Correct me if I'm wrong.

Thanks again to everyone for their help thus far.

Subject: Problem using a for loop to delete rows and columns from a

From: Nathan

Date: 28 Oct, 2009 19:23:55

Message: 13 of 16

On Oct 28, 12:05 pm, "Mark Hard" <GerryTheLe...@hotmail.com> wrote:
> Thanks for another reply.
>
> You see, in order for me to get the column vector, I need to invert the array after I have cut the rows and columns out of it and multiply it by a different column vector (m x 1) which also has values cut from it in order for the multiplication to be possible [ (m x m) x (m x 1) leaves an (m x 1) ]. This remaining m x 1 is the culumn vector I need to sub the 0 values back into the correct places in order for it to become an n x 1.
>
> So if I use that method and I get left with something like... (as mentioned above)
>
> k =
>      1 4 5 6 7 8
>      0 0 0 0 0 0
>      0 0 0 0 0 0
>     25 28 29 30 31 32
>     33 36 37 38 39 40
>     41 44 45 46 47 48
>     49 52 53 54 55 56
>     57 60 61 62 63 64
>
> This will not invert and I cannot work out the column vector I am trying to sub the 0's back into now. I hope I'm making sense. Correct me if I'm wrong.
>
> Thanks again to everyone for their help thus far.

You keep repeating yourself and I still don't understand your point.

What are you trying to do?
What do you mean by "invert". You don't mean the matrix inverse, do
you?

What are you looking for?

Do you mean transpose?
k(DOFc,:) = [];
k(:,DOFc) = 0;
k = k'
%%%%%%%%%%%%%%%%%%%
k =
     1 25 33 41 49 57
     0 0 0 0 0 0
     0 0 0 0 0 0
     4 28 36 44 52 60
     5 29 37 45 53 61
     6 30 38 46 54 62
     7 31 39 47 55 63
     8 32 40 48 56 64

k(:,1) %your requested column vector
%%%%%%%%%%
ans =
     1
     0
     0
     4
     5
     6
     7
     8


You have lost me.

Why not take the cyclist's idea and create two matrices, then?

k = original matrix
k1 = new square matrix missing rows and columns defined by DOFc
k2 = matrix only missing rows defined by DOFc, and zeroed columns as
defined by DOFc

to get your column vector(s), you would just transpose k2.

What is the point in inserting the zeros back in if you don't have to
in the first place?

-Nathan

-Nathan

Subject: Problem using a for loop to delete rows and columns from a

From: Mark Hard

Date: 28 Oct, 2009 19:42:01

Message: 14 of 16

Nathan <ngreco32@gmail.com> wrote in message <d4009cba-95ee-4353-b15d-ece65394b412@o9g2000prg.googlegroups.com>...

> to get your column vector(s), you would just transpose k2.

I don't transpose it, I invert it and for that it needs to be square. The creating more than one matrix was a good idea is it wasnt for this.

Basically im using this to do a finite element problem. the size of the original array shows degrees of freedom from the nodes. But some nodes are fixed so I cancel the corresponding rows and columns.

Eventually when I get the chopped down arrary I can use it to get the displacements of the nodes that do move (this is the column vector), but if I want to use the strain equation to get the strains I need to show that the displacement of the nodes that don't move is 0. I need to multiply the displacement column vector by a different array to get the strains, and I need the 0's back in for it to be the right size.

So basically I need to somehow put 0's into the column matrix without deleting the original value thats there. This will make the column matrix "taller" (my matrix terminology isnt the best) and allow it to be multiplied by another matrix to get the strains.

Hopefully that might clear some of this up.

Subject: Problem using a for loop to delete rows and columns from a

From: Nathan

Date: 28 Oct, 2009 19:56:03

Message: 15 of 16

On Oct 28, 12:42 pm, "Mark Hard" <GerryTheLe...@hotmail.com> wrote:
> Nathan <ngrec...@gmail.com> wrote in message <d4009cba-95ee-4353-b15d-ece65394b...@o9g2000prg.googlegroups.com>...
> > to get your column vector(s), you would just transpose k2.
>
> I don't transpose it, I invert it and for that it needs to be square. The creating more than one matrix was a good idea is it wasnt for this.
>
> Basically im using this to do a finite element problem. the size of the original array shows degrees of freedom from the nodes. But some nodes are fixed so I cancel the corresponding rows and columns.
>
> Eventually when I get the chopped down arrary I can use it to get the displacements of the nodes that do move  (this is the column vector), but if I want to use the strain equation to get the strains I need to show that the displacement of the nodes that don't move is 0. I need to multiply the displacement column vector by a different array to get the strains, and I need the 0's back in for it to be the right size.
>
> So basically I need to somehow put 0's into the column matrix without deleting the original value thats there. This will make the column matrix "taller" (my matrix terminology isnt the best) and allow it to be multiplied by another matrix to get the strains.
>
> Hopefully that might clear some of this up.

Would the use of NaN's be a problem rather than "chopping" the matrix
down?

If not, then you can just replace the NaN's by 0 in your column vector
for the displacements.

How about using this to re-insert zeros? (I haven't tried it, but it
looks fine)

http://www.mathworks.com/matlabcentral/fileexchange/9984-insertrows-v2-0-may-2008

-Nathan

Subject: Problem using a for loop to delete rows and columns from a

From: Mark Hard

Date: 28 Oct, 2009 20:28:02

Message: 16 of 16

I'n not really sure what effect putting in NaN's or 0's will have on inverting the matrix so I'll leave that option for now.

Thanks for the link, I'll try and get my head around it and come back here and post how I progress with it.

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com