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:
matrix column selection

Subject: matrix column selection

From: Wendy

Date: 8 Mar, 2010 14:19:05

Message: 1 of 4

Hi all,

I am a problem regarding selection of certain columns in a matrix. For example

A = [ 1 3 4 6
        4 8 9 7
        0 9 5 4];

I select the second column as x, i.e., x = [3 8 9]'. Is there a function that I can refer all the other columns as a matrix, for example,

y = [ 1 4 6
        4 9 7
        0 5 4];

Thank you,
Wendy

Subject: matrix column selection

From: Jos (10584)

Date: 8 Mar, 2010 14:40:28

Message: 2 of 4

"Wendy " <wlq121@gmail.com> wrote in message <hn310p$3cv$1@fred.mathworks.com>...
> Hi all,
>
> I am a problem regarding selection of certain columns in a matrix. For example
>
> A = [ 1 3 4 6
> 4 8 9 7
> 0 9 5 4];
>
> I select the second column as x, i.e., x = [3 8 9]'. Is there a function that I can refer all the other columns as a matrix, for example,
>
> y = [ 1 4 6
> 4 9 7
> 0 5 4];
>
> Thank you,
> Wendy

It depends on how you actually select the second column

A = [1 3 4 6 ; 4 8 9 7 ; 0 9 5 4] % original data

% 1) select it by a pre-defined index
  ColIdx = 2
  x = A(:,ColIdx)
  B = A % work on copy
  B(:,ColIdx) = [] % remove column

% 2) select it by pre-defined values
  x = [3 8 9].'
  tf = ismember(A.',x.','rows') % note the transposes
  B = A
  B(:,tf) = []

hth
Jos

Subject: matrix column selection

From: Herve

Date: 8 Mar, 2010 14:56:21

Message: 3 of 4

Hi, this function will do the job.
A is your input matrix.
i is the column that you want to extract
x and y are the outputs that you wish.

function [x,y] =wendy(A,i)

[m,n] = size(A);

x = A(:,i);

if i == 1
    yi = 2:n;
elseif i == n
    yi = 1:n-1;
else
    yi = horzcat(1:i-1,i+1:n);
end

y = A(:,yi);

end

Subject: matrix column selection

From: Husam Aldahiyat

Date: 8 Mar, 2010 15:27:22

Message: 4 of 4

"Wendy " <wlq121@gmail.com> wrote in message <hn310p$3cv$1@fred.mathworks.com>...
> Hi all,
>
> I am a problem regarding selection of certain columns in a matrix. For example
>
> A = [ 1 3 4 6
> 4 8 9 7
> 0 9 5 4];
>
> I select the second column as x, i.e., x = [3 8 9]'. Is there a function that I can refer all the other columns as a matrix, for example,
>
> y = [ 1 4 6
> 4 9 7
> 0 5 4];
>
> Thank you,
> Wendy

Use

>> A(:,~transpose(ismember(A',x','rows')))

For more info, type in

>> help ismember

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