user3535716
user3535716

Reputation: 255

Matlab, How to get each column in a matrix

I got a 4-by-n matrix, like

A =

 1     5     9
 3     0     6
 2     3    10
 7     8     4

What I want to do with A is getting each half column of A as

Line1Point1 = [1 3]

Line1Point2 = [2 7]

Line2Point1 = [5 0]

Line2Point2 = [3 8]

Line3Point1 = [9 6]

Line3Point2 = [10 4]

How could I do that? I’m pretty new to matlab coding.. Any help is really appreciated..

Cheers

Upvotes: 2

Views: 525

Answers (5)

RTL
RTL

Reputation: 3587

Storing such information as many variables is generally a bad idea

Some options for storing and accessing are

Cell array

Line=mat2cell(A,[2,2],ones(1,size(A,2))).'

access with

Line{2,1}
ans =

     5
     0

Indexing

as other answers

Anonymous Function

Line=@(l,p)A(2*p-1:2*p,l)

access with

Line(2,1)

ans =

     5
     0

Structure

Not really a useful solution, more for interests sake

for ii=1:size(A,2);for jj=1:2;Line(ii).Point(jj).Value=A(2*jj-1:2*jj,ii);end;end

access with

Line(2).Point(1).Value

ans =

     5
     0

Upvotes: 2

Divakar
Divakar

Reputation: 221684

I would suggest using 3D arrays to store and then access those values.

Code

N = size(A,1)/2;
LinePoint = permute(reshape(A,N,size(A,1)/N,[]),[1 3 2])

Here,

  • 2nd dimension indices (columns) would represent Line IDs
  • 3rd dimension indices would represent Point IDs.

Thus, the representative 3D array would be - LinePoint(:,LineID,PointID).

Example run

For your given A, we would have LinePoint as -

LinePoint(:,:,1) =
     1     5     9
     3     0     6
LinePoint(:,:,2) =
     2     3    10
     7     8     4

Thus,

Line1Point1 would be denoted by LinePoint(:,1,1)
Line1Point2 would be denoted by LinePoint(:,1,2)

Line2Point1 would be denoted by LinePoint(:,2,1)
Line2Point2 would be denoted by LinePoint(:,2,2)

Line3Point1 would be denoted by LinePoint(:,3,1)
Line3Point2 would be denoted by LinePoint(:,3,2)

Upvotes: 1

zinjaai
zinjaai

Reputation: 2385

You can create the variables with the eval function, which executes the input string. Using eval is commonly regarded as bad practice since it is horrible to debug. Nevertheless, here's the code:

A = [1 5 9; 3 0 6; 2 3 10; 7 8 4];
for ii = 1:length(A(1,:))
    eval(['Line' num2str(ii) 'Point1 = A(1:2, ii)' ]); 
    eval(['Line' num2str(ii) 'Point2 = A(3:4, ii)' ]);
end
% Now all variables are created - for example: Line2Point1 

A more elegant solution could be to store the vectors in a cell array. You can acces the first vectors for example by typing: c{1,1}

c = cell(length(A(1,:)),2)
for ii = 1:length(A(1,:))
    c{ii,1} = A(1:2, ii);
    c{ii,2} = A(3:4, ii);
end

Upvotes: 1

Deepu
Deepu

Reputation: 7610

A(1:2,1) will give you first half of the first column.

A(3:4,1) will give you second half of the first column.

A(1:2,2) will give you first half of the second column.

A(3:4,2) will give you second half of the second column.

A(1:2,3) will give you first half of the third column.

A(3:4,3) will give you second half of the third column.

Upvotes: 1

lakshmen
lakshmen

Reputation: 29094

Use reshape function, for example:

>> A =  [1     5     9;
 3     0     6;
 2     3    10;
 7     8     4];
>> reshape(A,2,6)
ans =

     1     2     5     3     9    10
     3     7     0     8     6     4

Upvotes: 6

Related Questions