MATLAB: How to save matrix in a loop which size are changing

image processingMATLAB

if jenis == 1
for k = 1;
positif(k,:)=feature(:);
k = k+ 1;
end
else
for j = 1;
negatif(j,:)=feature(:);
j = j+ 1;
end
end
I have a code above. When I input an image, the image taken its feature which form of matrix. I have to save the 'feature' in positif, and it will loop throughout I input another image. But I have a problem here, error in positif(k,:)=feature(:), Subscripted assignment dimension mismatch. Please help me.

Best Answer

That's not how you use a for loop - you'd better review that. But you don't even need a for loop. Assuming you've preallocated the positif and negatif arrays, you can simply to this:
if jenis == 1
positif(1, 1:numel(feature))=feature(:); % Store feature in row #1 of positif

else
negatif(1, 1:numel(feature))=feature(:); % Store feature in row #1 of negatif

end
If that doesn't work, try transposing feature:
if jenis == 1
positif(1, 1:numel(feature))=feature(:)'; % Store feature in row #1 of positif
else
negatif(1, 1:numel(feature))=feature(:)'; % Store feature in row #1 of negatif
end