T = [];
p=5;
for i =5:10
p=i+1;
T = data(p,11:74)
T(:,all(ismissing(T,0)))=[]
end
I have a big table where I need to read the entries row wise and remove all the col which has zero value. I am reading all the row using a loop . Kindly let me know the best.
var1 var2 var3 var4 var5
row1 0 1 0 0 1
var1 var2 var3 var4 var5
row2 1 1 0 0 1
results should be
var1 var2 var5
row2 1 1 1

 採用された回答

Kevin Phung
Kevin Phung 2019 年 3 月 14 日

0 投票

for an array, say:
a =
1 0 0
1 0 1
1 1 1
0 0 1
you can just do:
any(a==0,1)
to find all columns that contain a 0.

7 件のコメント

babyelephant
babyelephant 2019 年 3 月 17 日
Hi, Thanks for the suggestion.
Error is
Undefined operator '==' for input arguments of type 'table'.
Kevin Phung
Kevin Phung 2019 年 3 月 17 日
ok, then extract the data from the table with table2array
Walter Roberson
Walter Roberson 2019 年 3 月 17 日
T(any(T{:,:}==0,2),:) = [];
babyelephant
babyelephant 2019 年 3 月 18 日
I use the command table2array, good I can extract the all 1 from the array but again a new problem for a bigdata set how I will fin d the correct col name when I use array2table command.
Kevin Phung
Kevin Phung 2019 年 3 月 18 日
if T is your Table,
T.Properties.VariableNames
returns the column names of the table. So just do indexing for the columns you want.
if I had columns c1 c2 c3 c4 c5, and wanted to look at columns c2 and c5:
col = {'c1','c2'}
T{:,ismember(T.Properties.VariableNames,col)} % Walter's comment with curly brackets will replace the table2array command
Walter Roberson
Walter Roberson 2019 年 3 月 18 日
T{:,col}
The table indexing syntax permits a cell array of column names.
babyelephant
babyelephant 2019 年 3 月 20 日
data=pat(2:end,11:74); %change according to file
nrows=10;%change according to file
T = [];
val=data(1,1);
for i = 1:nrows
if i ==i
p=i;
T = data(p,:);
elseif val==data(i,1)
T = data(p+1,:);
end
T(:,all(ismissing(T,'')))=[]
col=T.Properties.VariableNames
T(any(T{:,:}==0,2),:) =[]
end
col=T.Properties.VariableNames;
coln=64;
valz=col{1}
for i = 1:64
if i ==i
s=i;
TZ = col{s};
T{:,ismember(T.Properties.VariableNames,TZ)}
end
end
I am new to MATLAB so may be my code looks not good. sorry for this.
Answer should be in a row wise loop for every row I should get only those col which contains one.

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

その他の回答 (2 件)

babyelephant
babyelephant 2019 年 3 月 20 日

0 投票

I can also use
[num,txt,raw] = xlsread('pat-test.xlsx');
raw{2,1}
num(2,1)
txt(1,:)
but then I can not find it out how I get the row without zero value col.

1 件のコメント

Walter Roberson
Walter Roberson 2019 年 3 月 20 日
num_not_empty = num(any(num,2),:);

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

babyelephant
babyelephant 2019 年 3 月 22 日

0 投票

Hello, Finally its done . Thank you all.
A=num(i,:); %get all the entries
A(isnan(A)) = 0; %convert NaN to zero
pos=find(A); %to find the possition of 1
[m,n] =size(pos);

カテゴリ

質問済み:

2019 年 3 月 14 日

回答済み:

2019 年 3 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by