how to store a series of single rows that have different numbers of columns
6 ビュー (過去 30 日間)
古いコメントを表示
I have a loop that creates a series of single rows that have different numbers of columns
I need to collect/store these as the loop iterates, and I dont know in advance what the widest row will be
which means I cant preallocate to the widest row, and pad less wide rows out to equal the widest row -- not sure how I would do this anyways... use resize?
at any rate believe the standard answer to this problem is to preallocate to some really wide number of cols, guessing that no new row will be wider than that, and then trim down the table once the rows are collected?
but matlab wont let me add rows unless the number of columns is the *same as (cant be less than) the preallocated matrix -- seems like a chicken and egg situation?
I suspect this is a common problem, and I did try to find the solution here, but nobody seems to be hitting this particular wall
I believe that if I had all the rows parked in different variables I could stack them [a(:);b(:);...;z(:)] but I cant do that either
thanks for your help
0 件のコメント
回答 (3 件)
Walter Roberson
2016 年 5 月 13 日
Cell array. This is fairly efficient to store the individual items. Afterwards you can go through and find the largest of them and use that length to pack everything else in to a rectangular numeric matrix if you want.
By the way: if you have a matrix that you want to add a row to, and the row might have a different number of columns than the existing matrix, then:
YourMatrix(end+1, 1:length(NewData)) = NewData;
If NewData is no wider than what is already there then this will not change the number of columns: it will just write what is needed, and will use 0 for any unused columns.
If NewData is wider than what is already there, then all of the other rows will be expanded out with trailing 0 to fit the new width.
Note: adding new rows this way is fairly inefficient! It would, for one thing, be more efficient to add new columns instead, even if that means building up by columns and later transposing:
YourMatrix(1:length(NewData), end+1) = NewData;
This is still not great as it keeps expanding the size of the matrix. If you know the maximum number of entries then you can do better by pre-allocating, in which case I think maybe row order might be more efficient
YourMatrix = zeros(NumberOfEntries, GuessOfMaximumSize);
YourMatrix(EntryNumber, 1:length(NewData)) = NewData;
... but using cell arrays and then putting everything together at the end would definitely be more efficient.
0 件のコメント
Azzi Abdelmalek
2016 年 5 月 13 日
You can use cell arrays
for k=1:5
idx=randi;
out{k}=rand(1,idx);
end
celldisp(out)
0 件のコメント
KSSV
2016 年 5 月 13 日
clc ; clear
N = 100 ;
iwant = cell(N,1) ;
for i = 1:N
iwant{i} = rand(1,randsample(1:100,1)) ;
end
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Resizing and Reshaping Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!