How to buffer old columns in a matrix when new columns are added

1 次查看(过去 30 天)
I'm writing a loop where I'm continually adding to an m x n matrix called histoMat on each iteration with a row vector called reposit. reposit is re-initialized and differs in size from iteration to iteration, but histoMat only grows, like so:
histoMat = [histoMat; reposit]
Unfortunately, I can't initialize the size of histoMat before the loop begins, since I don't know the maximum size of any single reposit row. As you can see, because reposit is very rarely the same size as it was the iteration before, I'm getting a vertcat dimension error when I try to append on to histoMat. For example, this will give me an error:
reposit = [1 2 3 4 5]
histoMat = [1 2 3]
histoMat = [histoMat; reposit]
Error using vertcat
Dimensions of matricies being concatenated are not consistent.
My question is: Is there any good way to buffer the bottom of histoMat with zeros whenever needed in order to avoid this? Here's an ideal example of what I'd like to accomplish:
reposit = [1 2 3 4 5]
histoMat = [1 2 3]
histoMat = [histoMat; reposit]
histoMat = [1 2 3 0 0
1 2 3 4 5]

采纳的回答

James Tursa
James Tursa 2017-10-9
编辑:James Tursa 2017-10-9
Append reposit this way:
histoMat(end+1,1:numel(reposit)) = reposit;

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by