필터 지우기
필터 지우기

cell contents in a loop_fangjun's answer

조회 수: 2 (최근 30일)
YOGESH
YOGESH 2011년 8월 15일
Hello all, I have a vars = cell(1,9) like following, the code works fine when there is a single row.
vars = {'OKR_evapo','MDF_albedo','HDU_wind','GDf_temp','MPGF_humidity', 'MDF_pressure','YKK_temperature','XRYO_rain','MPIO_radiation'};
files={'myd_11_MDF_albedo_a54576' 'mod_19_GDf_temp_vhk464567' 'mcd_13_MPGF_humidity.sdjfg590856' 'myd_11_MDF_pressure_46358' 'cyd_11_YKK_temperature_a54576' 'dod_13_XRYO_rain_vhk464567' 'ecd_11_MPIO_radiation.sdjfg590856'};
Index=false(size(vars));
for k=1:length(vars)
for j=1:length(files)
if strfind(files{j},vars{k})
Index(k)=true;
break;
end
end
end
How can I read all the rows in a loop if I have data/filenames in cell(10,9) instead of the single row. There are multiple rows in a cell of size 10*9. I should get a matrix of size 10*9 containing 1 and 0s.
  댓글 수: 2
Oleg Komarov
Oleg Komarov 2011년 8월 15일
Please format the code: http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup#answer_18099
Fangjun Jiang
Fangjun Jiang 2011년 8월 15일
Why do you ask the same question in two different places?

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

채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 8월 15일
You need to change the last part of the code as below.
Index=false(size(files));
for j=1:size(files,1)
for k=1:size(files,2)
for n=1:length(vars)
if strfind(files{j,k},vars{n})
Index(j,k)=true;
break;
end
end
end
end
Or looping this way to guess what is expected.
vars = {'ind' 'ger' 'us' 'swis' 'kor' 'ch'};
files = {'india' ,'germany','usa' ,'korea','','';...
'germany','usa' ,'china','' ,'','';...
'usa' ,'swiss' ,'china','' ,'',''};
N_row=size(files,1);
N_col=size(vars,2);
Index=false(N_row,N_col);
for j=1:N_row
for k=1:N_col
for n=1:size(files,2)
if strfind(files{j,n},vars{k})
Index(j,k)=true;
break;
end
end
end
end
Index =
1 1 1 0 1 0
0 1 1 0 0 1
0 0 1 1 0 1

추가 답변 (3개)

Oleg Komarov
Oleg Komarov 2011년 8월 15일
vars = {'ind' 'ger' 'us' 'swis' 'kor' 'ch'};
files = {'india' ,'germany','usa' ,'korea','','';...
'germany','usa' ,'china','' ,'','';...
'usa' ,'swiss' ,'china','' ,'',''};
szF = size(files);
Index = false(szF);
for n = 1:numel(vars)
[r,c] = find(~cellfun('isempty',strfind(files,vars{n})));
c(:) = n;
Index(sub2ind(szF,r,c)) = true;
end
  댓글 수: 1
YOGESH
YOGESH 2011년 8월 15일
Thanks Oleg.
This also works fine.

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


Jan
Jan 2011년 8월 15일
Replace "for k=1:length(vars)" by "for k=1:numel(vars)".

YOGESH
YOGESH 2011년 8월 15일
Thanks. but it seems that it is not doing well. e.g. I created a cell (of size (3,6) like
vars = {'ind' 'ger' 'us' 'swis' 'kor' 'ch'};
files = cell([{'india'},{'germany'},{'usa'},{'korea'},{''},{''};...
{'germany'},{'usa'},{'china'},{''},{''},{''};...
{'usa'},{'swiss'},{'china'},{''},{''},{''}]);
If I run the code, I should get
Index = [ 1 1 1 0 1 0;
0 1 1 0 0 1;
0 0 1 1 0 1 ]
all I am getting is like
[ 1 1 1 1 0 0; 1 1 1 0 0 0; 1 1 1 0 0 0]
@Oleg, thanks for the formating tutorial. cheers
  댓글 수: 7
Fangjun Jiang
Fangjun Jiang 2011년 8월 15일
To be honest, the OP's description and examples are not clear or even mis-leading. Anyway, I added another version which created the results expected. It is a tedious for-loop with strfind(). That's all.
Oleg Komarov
Oleg Komarov 2011년 8월 15일
@Fangjun: well, you're rigth about the confusion. In fact my solution works for the specific example just because files is padded with empty strings.
I guess it won't be difficult to preallocate Index with m from files and n from vars.

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by