2

Consider I have a vector a of size 5x1 and I pad zeros at the beginning of this vector. The number of zeros are generated using the randn function. Due to the randn, the vector is padded by a random number of zeros in a for loop. I would like to store these varying size vectors in a single matrix and I am unable to figure a way how to do this (other than fixing the size of the matrix before hand. Here is a MWE for the same:

a = rand(5,1)
for ii = 1 : 6
delay = round(abs(randn(1,1)));
shifted_a = [zeros(delay,1);a];
temp_mat(:,ii) = shifted_a    
end

In the second iteration, matlab will definitely throw an error due to the assignment mismatch at temp_mat(:,ii) = shifted_a. Is there a way I can have all these vectors in a matrix without having to fix the size of the matrix in advance.

smyslov
  • 1,279
  • 1
  • 8
  • 29
  • A matrix can only have a regular size. Maybe you should use `cells` instead, i.e. saving each of your shifted `vector` in one `cell`. – Nemesis Jul 03 '15 at 07:51

2 Answers2

2

Use a cell array instead.

a = rand(5,1);
for ii = 1 : 6
   delay = round(abs(randn(1,1)));
   shifted_a = [zeros(delay,1);a];
   temp_mat{ii} = shifted_a;  % // Use a cell array instead
end

And if you want to join them, you can use vertcat to make one long vector.

B=vertcat(temp_mat{:});
kkuilla
  • 2,226
  • 3
  • 34
  • 37
  • This seems like a good option, but once I have the cell array, then based on the size of the largest column of temp_mat, I need to increase the size of the other columns in temp_mat too, so that all the columns in temp_mat would be of the same size. This was the reason I was considering using matries . So I think `vertcat ` would not be useful in this case. – smyslov Jul 03 '15 at 08:08
  • But you need to decide the values to fill the smaller columns (i.e. putting zeros at the end). – Nemesis Jul 03 '15 at 08:10
  • You didn't say what you wanted to do with the values. You just said that you wanted so store a matrix of varying size. Cell arrays will do that for you. The `vertcat` was just me anticipating that you wanted to join them into one long vector. Padding them to match the biggest vector is a different question. – kkuilla Jul 03 '15 at 08:13
  • Sorry for the miscommunication. I would like to fill the smaller columns by appending zeros at the end to match the size of the largest column of the cell array and I am not sure how I should be doing that – smyslov Jul 03 '15 at 08:13
  • 1
    Ask a new question about that. – kkuilla Jul 03 '15 at 08:13
  • @smyslov See if [this](http://stackoverflow.com/questions/28026844/vec2mat-w-different-number-of-columns) or [this](http://stackoverflow.com/questions/27486493/reshape-row-wise-w-different-starting-ending-elements-number) helps. – Divakar Jul 04 '15 at 06:05
1

To complete the question based on @kkuillas answer:

You can find out the maximum length of the columns by

max_len = max(cell2mat(cellfun(@(x)size(x,1),temp_mat,'UniformOutput',false)));

and then create your final matrix

fin_mat = zeros(max_len,size(temp_mat,2));

for i = 1:length(temp_mat)
    fin_mat(1:size(temp_mat{i},1),i) = temp_mat{i};
end

(maybe the for-loop can be replaced...).

Nemesis
  • 2,324
  • 13
  • 23
  • 1
    Is it possible to replace `length`? You should never use `length` according to [chappjc](https://stackoverflow.com/users/2778484/chappjc). – kkuilla Jul 03 '15 at 08:35