MATLAB: Combine arrays dependent on conditions

combine arraysdefine empty arrayifMATLABreshape

I have the following code where depending on the user input, a 1 by 4 matrix will be generated called Ox, with values ranging from 0 to 1. In this example, the input was 0.2, 0, 0 and 0.8.
I also have a 1 by 256 array called Array with integers in order from 1 to 256.
Depending on the input from the user, if the Nth element in Ox is greater than 0, Array will be reshaped in a certain way to form AllOxN. For this specific code, AllOx1,2,3 and 4 will be a 1 by 64 array if their respective elements in Ox are greater than 0.
I would like to combine the reshaped arrays after the if statements. So in this example with only two elements in Ox being greater than 0, A will be a 1 by 128 array. This does not work though as when an element from Ox is 0, the respective AllOx is undefined.
I don’t want to pre-define the 1 by 64 arrays with values of 0, as that combined will give a 1 by 256 array as an output for A, rather than the desired 1 by 128 in this example.
Ox = [0.2 0 0 0.8];
Array = (1:256);
if Ox(1) > 0
AllOx1 = reshape(Array(1:4:end), 1, []);
elseif Ox(2) > 0
AllOx2 = reshape(Array(2:4:end), 1, []);
elseif Ox(3) > 0
AllOx3 = reshape(Array(3:4:end), 1, []);
elseif Ox(4) > 0
AllOx4 = reshape(Array(4:4:end), 1, []);
end
A = [AllOx1 AllOx2 AllOx3 AllOx4];
Is there any way to pre-define an array to be empty? Or is there a better way of combining the arrays, so in this example, combining AllOx1 and AllOx2 automatically, such that if the inputs were to change, the required arrays to be combined would be updated?
Thank you

Best Answer

Initialize them as empty arrays. Also, change the if conditions as shown in this code
Ox = [0.2 0 0 0.8];
Array = (1:256);
AllOx1 = [];
AllOx2 = [];
AllOx3 = [];
AllOx4 = [];
if Ox(1) > 0
AllOx1 = reshape(Array(1:4:end), 1, []);
end
if Ox(2) > 0
AllOx2 = reshape(Array(2:4:end), 1, []);
end
if Ox(3) > 0
AllOx3 = reshape(Array(3:4:end), 1, []);
end
if Ox(4) > 0
AllOx4 = reshape(Array(4:4:end), 1, []);
end
A = [AllOx1 AllOx2 AllOx3 AllOx4];