How to assign NaN for empty cells in an array?

조회 수: 5 (최근 30일)
Natasya Kamarudin
Natasya Kamarudin 2019년 4월 19일
댓글: Natasya Kamarudin 2019년 4월 23일
I need to produce a logical array from two sets of array. The first array (I named here as Icnum) is a 5x1 cell, while the second array (named moga) is a 3x1 cell. How do I make moga into a 5x1 cell and also assigning NaN for the empty cells? Below is the code I have so far:
T=readtable('todaylist.xlsx');
%T.Icnum = {'960521106352';'000202105002';'980719434321';'961212107564';'960116431234'}
moga = {'960521106352';'961212107564';'950321109876'};
A=size(T.Icnum,1);
D = num2cell((zeros(1,A))) ;
for i = 1:A
D(i) = moga(i); %error: index exceeds array bounds
i=i+1;
end
for idx = 1:numel(D)
D{idx}(cellfun(@isempty, D{idx})) = {nan};
end
Finalanswer=ismember(T.Icnum,D);
T.hadir=(Finalanswer);
writetable(T, 'todaylist.xlsx')
  댓글 수: 2
Walter Roberson
Walter Roberson 2019년 4월 19일
moga(end+1:A) = {nan};
Natasya Kamarudin
Natasya Kamarudin 2019년 4월 23일
Sorry for the late reply. Your code works perfectly. Thank you so much!

댓글을 달려면 로그인하십시오.

답변 (1개)

Andrei Bobrov
Andrei Bobrov 2019년 4월 19일
Finalanswer = ismember(T.Icnum,moga);
  댓글 수: 1
Natasya Kamarudin
Natasya Kamarudin 2019년 4월 23일
Not really what I was looking for but thank you for your input. Though I do use this line in the final working code, since I got rid of the variable D:
T=readtable('todaylist.xlsx');
%T.Icnum = {'960521106352';'000202105002';'980719434321';'961212107564';'960116431234'}
moga = {'960521106352';'961212107564';'950321109876'};
A=size(T.Icnum,1);
moga(end+1:A) = {''};
Finalanswer=ismember(T.Icnum,moga);
T.hadir=(Finalanswer);
writetable(T, 'todaylist.xlsx')

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by