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:
Removing a Column From a Matrix

Subject: Removing a Column From a Matrix

From: Kevin Ellis

Date: 20 Mar, 2012 17:48:11

Message: 1 of 3

Hello,

I have a 2x106 matrix where a portion of the matrix is given as:

H = [-177.22469 -177.22469 -175.73394 -175.51375 -175.51375 -173.90222 -173.90222; 33.35329 22.26457 8.80863 13.90066 12.42554 11.89841 9.24061]

And looks like,

Columns 1 through 3

                -177.22469 -177.22469 -175.73394
                  33.35329 22.26457 8.80863

  Columns 4 through 6

                -175.51375 -175.51375 -173.90222
                  13.90066 12.42554 11.89841

  Column 7

                -173.90222
                   9.24061

I want to create a code that will take this matrix and first find values on row 1 that are equal and then compare their values on row 2 to find which is greater and then remove the column containing the smaller value on row 2 from the matrix. For example, the values on row 1 for Columns 1 and 2 are equal (-177.22469). It is obvious that the value on row 2 is greater for column 1 than column 2. Thus I want to remove/delete column 2 from the matrix.

Then I would like to continue in this manner comparing column values for the entire matrix. In this case Columns 4 and 5 would be next where Column 5 would be then be removed.

I have tried using for loops, but columns with equal values in Row 1 seem to always slip through. I have looked at diff and unique as well, but still having a hard time because of my limited understanding of MatLab.

Any help would be much appreciated.

Kevin Ellis

Subject: Removing a Column From a Matrix

From: Roger Stafford

Date: 20 Mar, 2012 22:19:11

Message: 2 of 3

"Kevin Ellis" wrote in message <jkafsr$438$1@newscl01ah.mathworks.com>...
> I want to create a code that will take this matrix and first find values on row 1 that are equal and then compare their values on row 2 to find which is greater and then remove the column containing the smaller value on row 2 from the matrix. .....
- - - - - - - -
  See if this works. I prepared it in haste.

 [~,p] = sort(H(2,:));
 [~,m] = unique(H(1,p),'last');
 Hnew = H(:,sort(p(m)));

Roger Stafford

Subject: Removing a Column From a Matrix

From: Roger Stafford

Date: 21 Mar, 2012 01:38:31

Message: 3 of 3

"Roger Stafford" wrote in message <jkavov$sef$1@newscl01ah.mathworks.com>...
> "Kevin Ellis" wrote in message <jkafsr$438$1@newscl01ah.mathworks.com>...
> > I want to create a code that will take this matrix and first find values on row 1 that are equal and then compare their values on row 2 to find which is greater and then remove the column containing the smaller value on row 2 from the matrix. .....
> - - - - - - - -
> See if this works. I prepared it in haste.
>
> [~,p] = sort(H(2,:));
> [~,m] = unique(H(1,p),'last');
> Hnew = H(:,sort(p(m)));
>
> Roger Stafford
- - - - - - - - -
  Here are some additional comments on your problem.

1. I assumed in the code I sent that columns possessing equal numbers in the first row do not have to be adjacent for there to be a deletion of the one with the least element in the second row.

2. In case both elements of a pair are repeated in two different columns, the code I sent will select the earlier one to be deleted. If you want it to be the later one, change the first sort to 'descending' and use 'first' in the 'unique' call.

3. You should realize that the 'unique' function will consider two elements unequal even if their least bits differ. Hence if numbers like -177.22469 are the result of computation, such results may look equal in displays but can nevertheless be considered unequal by 'unique'.

Roger Stafford

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