在MATLAB中,我有一个这样的表:
//project text to ASCII Dec
#include <stdio.h>
main(){
char i=0, data, a;
printf("Enter data : ");
scanf("%s", &data); //or %c
while(data != i){ /* can't out from this loop I mean to the next letter of word */
a = data;
printf("%d", data); // or printf("%d", a);
if(data != 0){
printf("-");
}
}
}
如何将date foo bar mitzva baz
1995-1-1 21 54 12 37
1995-1-2 NaN 52 11 36
1995-1-3 39 56 NaN 38
1995-1-4 25 NaN NaN 50
的任何列设置为其行NaN
的值?
或换句话说:
如何设置过滤器,以便在baz
之前的三列中的任何一列中逐行检查NaN
?
如何设置三个值而不复制好值3次?
为了让它发挥作用,我在下面做了哪些改变?还是我的方向错了?
baz
我想得到:
colsToUpdate = {colidxFoo : colidxMitzva);
baddataFilter = any(isnan(data(:,colsToUpdate));
% corrected from isnull. thanks @SardarUsama my mistake...
data(baddataFilter, colsToUpdate) = data(baddataFilter, colidxBaz);
答案 0 :(得分:1)
Val = table2array(T(:,2:end)); %Extracting the elements of all columns except 1st one
IndBadV = isnan(Val); %Finding NaNs
Val(IndBadV) = 0; %Replacing NaNs with zeros
Val = Val+repmat(Val(:,end),1,size(Val,2)) .* IndBadV; %Updating the values
T = [T(:,1) array2table(Val, ...%Updating the table
'VariableNames',T.Properties.VariableNames(2:end))];
答案 1 :(得分:-1)
只是为了澄清乌萨马的答案:
% problemCols = 2:end
Data = table2array(T(:,2:end)); % Without date column (which serves as index)
BadOnes = isnan(Data); % Finding NaNs
Data(BadOnes) = 0; % Replacing NaNs with zeros
FixcolsCount = size(Data,2); % Number of columns to fix
GoodCol = end;
RepFixed = repmat(Data(:,GoodCol),1,FixcolsCount); % repeat 1 time down, for all columns
Data = Data + RepFixed .* BadOnes; % Updating the values
T = [T(:,1) array2table(Val, ... % Updating the table
'VariableNames',T.Properties.VariableNames(2:end))];
注意:这是有效的,因为当数据存在时添加了零(badones为零),但是当数据不存在时添加了Repfix值(在这种情况下,badones为1)。
例如:
index foo bar mitzva baz badones a b c fixed foo bar mitzva
1 11 na 13 14 0 1 0 0 14 0
2 na 22 23 24 1 0 0 24 0 0
加法结果和。*乘法:
1 11 14 13 14
2 24 22 23 24
一位同事用另一种方式告诉我:
BadOnes = isnan(T{:,2:end-1});
GoodIndice = find(~BadOnes);
Fixed = BadOnes * repmat(T{:,end},1,size(T{:,2:end-1},2));;
Fixed(GoodIndice) = DataArray(GoodIndice); % adds GoodValues back into array
T{:,2:end-1} = Fixed;
或详细解释:
DataArray = T{:,2:end-1};
ProblemColsCount = size(DataArray,2); % Number of columns to fix
BadOnes = isnan(DataArray); % Finding NaNs
% GoodCol = end;
RepFixed = repmat(T{:,end},1,ProblemColsCount); % repeat 1 time down, for all columns
GoodIndice = find(~BadOnes);
Fixed = BadOnes * RepFixed;
Fixed(GoodIndice) = DataArray(GoodIndice); % adds GoodValues back into array
T{:,2:end0-1} = Fixed;
在任何情况下,我都希望有一些像Usama的原始答案fillmissing()
或类似的东西。