2015-06-29 173 views
-8

我有一個矩陣。我想從矩陣中獲得一個向量,如下例所示:將矩陣轉換爲矢量

Matrix = [ 2 4 5; 
      8 2 13; 
      0 3 1; 
      7 7 7; 
      36 62 72; 
      44 35 26; 
      63 11 4; 
      9 9 9 ]; 

vector = [ 2 8 0 4 2 3 5 13 1 7 36 44 63 62 35 11 72 26 4 9]; 

向量插入每列中前三行的每個值。然後它插入第四行值一次。然後,對矩陣中的其餘元素重複該過程。如何在Matlab中做到這一點?

+1

你覺得重塑時,與一個for循環?無論如何,你應該展示你迄今爲止的嘗試。這樣做我和你將有更好的機會在這裏獲得幫助。 – patrik

+0

你的描述說你以某種方式重新排列元素,但矩陣和向量包含不同的元素。 – Daniel

+0

我嘗試重塑矩陣,但它不起作用。 – Dani

回答

0

你的問題是一個非常具體的問題。我不明白這對除了你自己以外的人有什麼用處。 沒有「單線解決方案」。 有很多方法可以解決索引問題,我喜歡用標索引時可能:

Ncolumns = size(Matrix,1); 
Nblocks = floor(Ncolumns/4);         %number of 4-line blocks (excluding the last block if it is not a full 4-lines) 
IndexVector = (1:3)'*ones(1,3) + ones(3,1)*(0:2) * Ncolumns; %this gives 3 lines as specified. 
IndexVector = [IndexVector(:); 4];       %this adds the first element of 4th line, as spec. 
IndexVector = IndexVector*ones(1,Nblocks)+ones(10,1)*(0:Nblocks-1)*4; %this repeats the above for rest of blocks. 
IndexVector = IndexVector(:)';        %make row vector 

vector=Matrix(IndexVector); 

if mod(Ncolumns,4)        %this deals with the last partial block 
    subMat=Matrix(Nblocks*4+1:end,1:3); 
    vector=[vector subMat(:)']; 
end