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

5 Ansichten (letzte 30 Tage)
hadiqa khan am 16 Mai 2018
Kommentiert: hadiqa khan am 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
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### 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);
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 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
hadiqa khan am 23 Mai 2018
hey thanks this really worked!!! :))))

Melden Sie sich an, um zu kommentieren.

### 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;
end
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

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