Filter löschen
Filter löschen

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

5 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

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

Weitere Antworten (1)

Guillaume am 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;


Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange


Community Treasure Hunt

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

Start Hunting!

Translated by