Seperate column into more columns: 90740x1 double array into 1511x64 something array

1 回表示 (過去 30 日間)
Hello
i have a set of data 90740x1 double. meaning:
what i really want this to do is make a new matrix where i read every 1511 data from this row into a new row. example:
so from 1 to 1511 in column 1 in the new matrix...and from 1511 to 3023 into column 2 in the new matrix etc. etc.
i made a code, it only works for the first 1511 data but after that it displays an error about dimension.
n = 1; for i=1:length(kwh) kunder(:,1) = kwh(1:n:1511); end
so when u put: kunder(:,1) = kwh(1511:n:3023); under the first kunder it displays dimension error...
please help me figure this out..as i want to plot the 24 hour kwh usage data of customers consumption of electricity.
thank you

採用された回答

Kelly Kearney
Kelly Kearney 2014 年 4 月 8 日
Your code is erroring because of mismatched dimensions; when n = 2, 1:n:1511 expands to [1 3 5 ... 1511], which is only 756 elements in length. What you wanted was [1:1511]+1511*(n-1). But it would be more straighforward just using reshape.
x = rand(90740,1);
nrow = 1511;
ncol = ceil(length(x)/nrow);
x = [x; nan(ncol*nrow-length(x),1)];
x = reshape(x,nrow,ncol);
Note that you need to pad with NaNs or 0s or whatever is appropriate, since your number of elements isn't evenly divisible by 1511.
  2 件のコメント
awda
awda 2014 年 4 月 8 日
Thank you, it really did work, it helped me :)
there is a small problem i dont understand now: when i divide 90740/1511 i should get 64 columns with 34 more data in the last part..but now i get 300+ more data in the column 65..with nan in the end.. any ideas if it is dividing the Column correctly or adding random data in ?
Kelly Kearney
Kelly Kearney 2014 年 4 月 8 日
How do you figure that? 90740/1511 = 60.053, ie. 60 remainder 80. So you should end up with a 1511 x 61 array, with 80 elements in the final column.

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

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 4 月 8 日
Use reshape
reshape(A,1511,64)
  3 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 4 月 8 日
This is because with 90740 elements you can't get a 1511x64 array which contains 96704 element
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 4 月 8 日
What you can do is to add some nan to your final array
n=90740
A=rand(n,1);
m=ceil(90740/1511)*1511
A(end+1:m)=nan
out=reshape(A,1511,[])

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by