Effacer les filtres
Effacer les filtres

replace 0 with NaN for specific column in a matrix within cell

3 vues (au cours des 30 derniers jours)
hadiqa khan
hadiqa khan le 16 Mai 2018
Commenté : hadiqa khan le 23 Mai 2018
my data is of 1x12 cells and 600x4 matrix in each cell but i want to replace 0 with NaN only in 4th column of matrix in each cell data{1,t}(data{1,t}(:,4)==0)=0 this command doesnt convert 0 to NaN While if i try this: data{1,t}(data{1,t}==0)=0 it converts 0 from all columns to NaN which is not desirable

Réponse acceptée

Jan
Jan le 16 Mai 2018
% Some test data:
data = cell(1, 12);
for k = 1:12
data{k} = randi([0,2], 600, 4);
end
% Replace 0 by NaN in 4th column:
for k = 1:numel(data)
col4 = data{k}(:, 4);
col4(col4 == 0) = NaN;
data{k}(:, 4) = col4;
end

Plus de réponses (1)

Guillaume
Guillaume le 16 Mai 2018
Since all your matrices are the same size, the easiest would be to get rid of the cell array and store all your matrices as a single 3D matrix. This is easier and faster:
data_mat = cat(3, data{:});
temp = data_mat(:, 4, :);
temp(temp == 0) = nan;
data_mat(:, 4, :) = temp;
Otherwise, you'll have to use an explicit loop:
for iter = 1:numel(data)
temp = data{iter}(:, 4);
temp(temp == 0) = nan;
data{iter}(:, 4) = temp;
end

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by