フィルターのクリア

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

3 ビュー (過去 30 日間)
hadiqa khan
hadiqa khan 2018 年 5 月 16 日
コメント済み: hadiqa khan 2018 年 5 月 23 日
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 件のコメント
hadiqa khan
hadiqa khan 2018 年 5 月 23 日
hey thanks this really worked!!! :))))

サインインしてコメントする。

その他の回答 (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 Center および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by