## How can I increase the speed of this loop?

Ash Ahamed

### Ash Ahamed (view profile)

님이 질문을 제출함. 18 Oct 2019
최근 활동 Ash Ahamed

### Ash Ahamed (view profile)

님이 댓글을 추가함. 18 Oct 2019
Jeremy Hughes

### Jeremy Hughes (view profile)

님이 답변을 채택함.
This loop takes a long time to finish. Is there anyway I can increase the speed? Thanks
for n=0:5039
name = sprintf('%d/conformability.txt', n)
fid = fopen(name) ;
data = cell2mat(data)
n = length(data)
data = reshape(data,[10,n/10])
data = transpose(data)
fclose(fid);
comformability(:,n)=data(:,2)
data = []
end

로그인 to comment.

R2018b

## 답변 수: 1

Jeremy Hughes

### Jeremy Hughes (view profile)

님의 답변 18 Oct 2019
Jeremy Hughes

### Jeremy Hughes (view profile)

님이 편집함. 18 Oct 2019
채택된 답변

It's hard to say without looking at the contents of the file's you're reading.
You're overriding n in the for loop with n as the length of the data. Is that a typo?
fmt = repmat({'%*f32'},1,10);
fmt{2} = '%f32';
fmt = [fmt{:}];
for K=0:5039
name = sprintf('%d/conformability.txt', K);
fid = fopen(name);
fclose(fid);
comformability(:,K+1)=data{1}; % if K == 0 this would error.
end

Ash Ahamed

### Ash Ahamed (view profile)

18 Oct 2019
Thanks! I wll try this out. n was a typo.
Jeremy Hughes

### Jeremy Hughes (view profile)

18 Oct 2019
I also made a typo... My "n" in the sprintf line should be "K"
Ash Ahamed

### Ash Ahamed (view profile)

18 Oct 2019
Saw that! haha thanks and it worked!!

로그인 to comment.

Translated by