Row vector, array matrix

1 回表示 (過去 30 日間)
Francesco
Francesco 2014 年 2 月 21 日
編集済み: Mischa Kim 2014 年 2 月 21 日
You can read this link. The code is good for the 1x72 double but it's not a general code because I could have 1xn double. How can I do in this situation? The question is that it's not always a 8x8 matrix but it's depend on the number of zero in the 1xn double. The problem is that I have in general 1xn double and not 1x72. So the reshape (8,8) it's not good in general. The important thing is that when there is a zero value, it's start a new rows: always. This is the important thing.
  2 件のコメント
Mischa Kim
Mischa Kim 2014 年 2 月 21 日
編集済み: Mischa Kim 2014 年 2 月 21 日
Does the zero entry appear periodically? That is every nth element is zero?
The issue here is that if there is no periodicity you end up with rows of different length, which you cannot save in a matrix (without filling up the matrix with zeros). In that case you would have to use e.g. a cell array.
Francesco
Francesco 2014 年 2 月 21 日
Yes, the zero appear periodically. After you see the first zero value, it appears with a certain periodicity every time.

サインインしてコメントする。

採用された回答

Mischa Kim
Mischa Kim 2014 年 2 月 21 日
編集済み: Mischa Kim 2014 年 2 月 21 日
OK. Provided that the zeros appear periodically just the way you have shown in your code, you could use
indz = find(A==0); % find positions of zeros in A
numr = indz(2)-indz(1)-1; % compute the constant(!) row lengths
A(A == 0) = [];
B = reshape(A,numr,length(A)/numr)
Again, if there is not a constant number of non-zero elements in each interval (that includes also the last interval) you end up with different row lengths. In that case you cannot build a matrix that satisfies your requirements.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by