MATLAB: Every iteration gives me a column vector of non zero elements. How to store them, all the columns, in either a single vector or an array? Attached is the sample code.

store non zero elements

close all; clear; clc;
STORE = nan(30,2);
for k = 1:2
rows = 4; columns = 3;
x = randi([0 3],rows,columns);
store = zeros(rows,columns);
Xstore = nan(30,2);
for j = 1:3
for i = 1:rows
store(i,j) = x(i,j);
%Xstore = store(store>0)
Xstore = nonzeros(store)
STORE(:,k) = Xstore; % This step does not walk, please
end
end
end

Best Answer

The dimensions of what you want to store are varying...use cell instead.
close all; clear; clc;
STORE = cell(2,1);
for k = 1:2
rows = 4; columns = 3;
x = randi([0 3],rows,columns);
store = zeros(rows,columns);
Xstore = nan(30,2);
for j = 1:3
for i = 1:rows
store(i,j) = x(i,j);
%Xstore = store(store>0)
Xstore = nonzeros(store) ;
STORE{j,i,k} = Xstore; % This step does not walk, please
end
end
end