Program not generating 0 as expected to turn variable cell to double
Mostrar comentarios más antiguos
I got a variable named SNR_BD_Mean_4, as I would like to find the mean in each of the rows in variable SNR_BD_Mean_4, I will need to change it from call to double. The number of each row of cell in SNR_BD_Mean_4 is different thus I will need to add 0 in other rows to make every row with the same size to turn it into a double variable. So, I come up with this code.
Z1 = cellfun(@numel, SNR_BD_Mean_4); % Converting variable from cell to double
Z2 = 9; % From Z1, input the highest number shown in Z1
newZ1 = Z2*ceil(Z1/Z2);
padfun = @(k) [SNR_BD_Mean_4{k} zeros(1, newZ1(k) - Z1(k))];
Z3 = arrayfun(padfun, 1:numel(SNR_BD_Mean_4), 'un', 0);
Z4 = reshape(Z3, size(SNR_BD_Mean_4));
SNR_BD_Mean = cell2mat(Z4);
SNR_BD_Mean(SNR_BD_Mean==0)=NaN;
The code runs well initially, but if there is a row in Z3 where there is no data, that particular row will not copy any 0 causing the size of each row in Z3 is not the same. This caused when Z4 is generated from reshaping Z3 and change from cell to double, the error will be triggered. Is there any method to fix the code so that whenever there is no data in a particular row, the code can still run successfully? Thanks.
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Data Type Identification en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!