Make my code for taking norm efficient
조회 수: 9 (최근 30일)
이전 댓글 표시
here is my code that is taking norm between 2 rows from 2 different excel files,it picks one row from my_first_file.xlsx and take norm with all the rows in my_second_file.xlsx and and store all the values to R i.e array/ vector and after completing one inner loop it takes the minimum value and its index from the array R,it runs OK but it takes lot much time to execute,please amend it in such a way that it takes considerably less time and performs efficiently and correct me where i am wrong,you are welcome.
n=853;m=500;
for s=1:n % n=number of rows in my_first_file.xlsx
filename = 'my_first_file.xlsx';
row1 = xlsread(filename,strcat('E',num2str(s),':EB',num2str(s)));
for i=1:m % m=number of rows in my_second_file.xlsx
filename1 = 'my_second_file.xlsx';
row2 = xlsread(filename1, strcat('A',num2str(i),':DX',num2str(i)));
sqE=norm(row2-row1);
R(i) = sqE;
end
[Val Ind] = min(R);
ran=sprintf('%c%d','A',s);
xlswrite(Hello, [Val, Ind],resultsheet,ran);
end
댓글 수: 0
채택된 답변
Mischa Kim
2014년 9월 8일
편집: Mischa Kim
2014년 9월 8일
Muahmmad, why don't you read in all data from the Excels files at once (in other words you are only calling xlsread twice) and save the data in matrices? If available (Parallel Computing Toolbox, that is), use parfor instead of a for-loop.
댓글 수: 3
Mischa Kim
2014년 9월 8일
Use something like
mat1 = xlsread('ex1.xlsx'); % define the range for your files appropriately
mat2 = xlsread('ex2.xlsx'); % define the range for your files appropriately
for ii = 1:size(mat1,1)
for jj = 1:size(mat2,1)
norm_12(jj + (ii-1)*size(mat2,1)) = norm(mat1(ii,:)-mat2(jj,:));
end
end
추가 답변 (1개)
Joseph Cheng
2014년 9월 8일
before the for loop read all the data from row 1 to row m from my_second_file.xlsx into matlab and then reference that data in the loop. it should reduce the time from opening and reading from the excel file in each for loop.
댓글 수: 2
Joseph Cheng
2014년 9월 8일
you basically did so before. just as you're reading in one line at a time. read in the whole thing like Mischa Kim described. so instead of reading in each row read in the whole block
XLS1= xlsread(filename,strcat('E',num2str(1),':EB',num2str(n)));
XLS2 = xlsread(filename1, strcat('A',num2str(1),':DX',num2str(m)));
Then you you can reference them like any 2D array in matlab.
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!