将矩阵分解成列向量并将其存储在数组中。

时间:2021-09-28 21:46:12

My question has two parts:

我的问题有两部分:

  1. Split a given matrix into its columns
  2. 将一个给定的矩阵分解为它的列。
  3. These columns should be stored into an array
  4. 这些列应该存储在一个数组中。

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

这些列是通过tempCol = A(:,iter)获得的,其中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数组的不同的索引),但是一个完整的a列应该存储在splitArray中的一个索引中:

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

and so on...

等等……

for the example matrix A,

对于矩阵A,

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

arraySplit(1)应该给我[1 3 4 6]。

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

arraySplit(2)应该给我[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 ...

我正在做arraySplit错误的分配和访问,请帮我…

3 个解决方案

#1


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.

编辑:请参见@EitanT的评论,以更优雅的方式完成此任务。同样,访问vector也使用与设置相同的语法,例如v = C {2};将A的第二列变成v。

#2


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.

在Matlab数组中,每个元素必须具有相同的类型。在大多数情况下,这是一个浮点类型。你的例子A(: 1)是一个4×1的数组。如果你把它赋值给,比方说,B(:, 2)然后B(:, 1)也必须是一个4×1的数组。

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.

一个常见的错误是,一个4×1的数组和一个1×4的数组是不一样的。一个是列向量,一个是行向量。尝试转置A(:, 1)得到一个1×4的行数组。

#3


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).

然后调用arraySplit(::,1)来获得第一个向量,但这似乎是一个不必要的步骤,因为您可以通过访问与A(: 1)完全相同的值来轻松实现这一步。

#1


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.

编辑:请参见@EitanT的评论,以更优雅的方式完成此任务。同样,访问vector也使用与设置相同的语法,例如v = C {2};将A的第二列变成v。

#2


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.

在Matlab数组中,每个元素必须具有相同的类型。在大多数情况下,这是一个浮点类型。你的例子A(: 1)是一个4×1的数组。如果你把它赋值给,比方说,B(:, 2)然后B(:, 1)也必须是一个4×1的数组。

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.

一个常见的错误是,一个4×1的数组和一个1×4的数组是不一样的。一个是列向量,一个是行向量。尝试转置A(:, 1)得到一个1×4的行数组。

#3


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).

然后调用arraySplit(::,1)来获得第一个向量,但这似乎是一个不必要的步骤,因为您可以通过访问与A(: 1)完全相同的值来轻松实现这一步。