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

3 次查看(过去 30 天)
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

采纳的回答

Jan
Jan 2018-5-16
% 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

更多回答(1 个)

Guillaume
Guillaume 2018-5-16
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

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by