0

My question has two parts:

  1. Split a given matrix into its columns
  2. These columns should be stored into an array

eg,

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

Now, I know the solution to the first part:

the columns are obtained via tempCol = A(:,iter), where iter = 1:end

Regarding the second part of the problem, I would like to have (something like this, maybe a different indexing into arraySplit array), but one full column of A should be stored at a single index in splitArray:

arraySplit(1) = A(:,1)
arraySplit(2) = A(:,2)

and so on...

for the example matrix A,

arraySplit(1) should give me [ 1 3 4 6 ]'

arraySplit(2) should give me [ 3 5 5 8 ]'

I am getting the following error, when i try to assign the column vector to my array.

In an assignment  A(I) = B, the number of elements in B and I must be the same.

I am doing the allocation and access of arraySplit wrongly, please help me out ...

Mehrwolf
  • 8,208
  • 2
  • 26
  • 38
prashant
  • 1
  • 1
  • 1
  • 2
  • A matrix is already an _array of vectors_, why do you need this unnecessary `arraySplit` sophistication? – Eitan T Sep 05 '12 at 06:55
  • What usually strikes new matlab users is the total lack of pointers: see [this](http://stackoverflow.com/q/466972/1499402) and [this](http://stackoverflow.com/q/6002257/1499402) questions. Apart from cell arrays, which are not that useful imho, you have to live without pointers and adapt your coding style to this constraint. In fact what you are asking for is an array of pointers so that `A(i,j)==arraySplit(j)(i)` but this is not possible with `()` parentheses. – Stefano M Sep 05 '12 at 12:31
  • @StefanoM I find every part of your comment **wrong**: 1) Not all "new MATLAB users" come from a C background, so saying that they are missing the functionality of memory locations and pointers is not necessarily true. 2) cell arrays are VERY useful to hold data of variable type as one collection, such as both numbers and strings, or vectors of different length. 3) Why are you certain the OP wants pointers rather than a _copy_ of these vectors? – Eitan T Sep 06 '12 at 11:07
  • @EitanT sorry for causing so much disapproval from you. Basically you are right, my brief comment assumes that when the OP is asking _"one full column of A should be stored at a single index in splitArray"_ he is asking for a C-like array of arrays. As what regard cell arrays, I should have written that they are not so useful as pointer substitutes. The sense of my comment was simply to make aware the OP that in MATLAB the data model for matrices (and N-dimensional array) is very different from C and numpy. – Stefano M Sep 06 '12 at 16:55
  • @StefanoM I didn't mean to sound aggressive, but it was important to point out this information in order not to mislead the OP. – Eitan T Sep 06 '12 at 17:14

3 Answers3

1

Really it sounds like A is alread what you want--I can't imagine a scenario where you gain anything by splitting them up. But if you do, then your best bet is likely a cell array, ie.

C = cell(1,3);
for i=1:3
   C{i} = A(:,i);
end

Edit: See @EitanT's comment below for a more elegant way to do this. Also accessing the vector uses the same syntax as setting it, e.g. v = C{2}; will put the second column of A into v.

Matt Phillips
  • 9,465
  • 8
  • 44
  • 75
  • 1
    You can shorten your answer to `C = mat2cell(A, size(A, 1), ones(1, size(A, 2)))`. Also, you should probably elaborate on how to access each column using the curly braces. – Eitan T Sep 05 '12 at 06:56
0

In a Matlab array, each element must have the same type. In most cases, that is a float type. An your example A(:, 1) is a 4 by 1 array. If you assign it to, say, B(:, 2) then B(:, 1) must also be a 4 by 1 array.

One common error that may be biting you is that a 4 by 1 array and a 1 by 4 array are not the same thing. One is a column vector and one is a row vector. Try transposing A(:, 1) to get a 1 by 4 row array.

guyrt
  • 927
  • 7
  • 12
0

You could try something like the following:

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

arraySplit = zeros(4,1,3);

for i =1:3
    arraySplit(:,:,i) = A(:,i);
end

and then call arraySplit(:,:,1) to get the first vector, but that seems to be an unnecessary step, since you can readily do that by accessing the exact same values as A(:,1).

Malife
  • 303
  • 2
  • 6