I wrote a %note beside each line in the code part to indicate exactly what changes in every increment. I hope it makes it clearer.
How to re-write these multiple codes in a single code?
2 ビュー (過去 30 日間)
古いコメントを表示
Hi,
I am getting the interpolation of 400 data from 100 plots. I have built one code and duplicated it into 400 copies in the same page to run the interpolation automatically. I would like to ask if it is possible to make these multiple codes only one, as I realized that the run time is too long. I am giving two sets of the codes below. The interpolation steps are as follows: The steps are as follows:
1) Plot z1 = x1,y1 data; Interpolate the first column data from z1
2) Plot z2 = x2,y2 data; Interpolate the first column data from z2;
This will continue until the 100 plots finish. Then the same process starts for the second column, and so on until the 400th column.
In every step, the following part of code is the only part that changes in increments:
b=A(:,2); %ith column is assigned to b variable at every step (in this step, it is 2)
y2 (:, 2) = b; %ith column (in this step, it is 2)
x2 (k,:) = interp1(y3, x3, y2(:,2), 'linear'); %ith column (in this step, it is 2)
temp=x2(k,:);
temp(isnan(temp))=0.05;
x2(k,:)=temp;
fid=fopen(['result_' num2str(2) '.txt'],'w');%ith column (in this step is is 2)
fprintf(fid,'%f\n',x2);
fclose(fid);
end
end
The two sets of codes are as follows:
Code 1%%
clear; clc;
Folder = cd;
N=100;
x2 = zeros(N, 10);
for k = 1:N;
Driftt = sprintf('Drift%d.out', k);
Reactt = sprintf('React%d.out', k);
matDrift = importdata(fullfile(Folder, Driftt));
matReact = importdata(fullfile(Folder, Reactt));
x1= matDrift(:,2);
y1= -sum(matReact(:,2:11),2);
[x3, ix] = unique(x1);
y3 = y1(ix);
A=dlmread('AllPiers2.txt');
for i=1:size(A,2)%number of columns
b=A(:,1);%ith column is assigned to b variable at every step
y2 (:,1) = b;
x2 (k,:) = interp1(y3, x3, y2(:,1), 'linear');
temp=x2(k,:);
temp(isnan(temp))=0.05;
x2(k,:)=temp;
fid=fopen(['result_' num2str(1) '.txt'],'w');
fprintf(fid,'%f\n',x2);
fclose(fid);
end
end
Code 2%%
clear; clc;
Folder = cd;
N=100;
x2 = zeros(N, 10);
for k = 1:N;
Driftt = sprintf('Drift%d.out', k);
Reactt = sprintf('React%d.out', k);
matDrift = importdata(fullfile(Folder, Driftt));
matReact = importdata(fullfile(Folder, Reactt));
x1= matDrift(:,2);
y1= -sum(matReact(:,2:11),2);
[x3, ix] = unique(x1);
y3 = y1(ix);
A=dlmread('AllPiers2.txt');
for i=1:size(A,2)%number of columns
b=A(:,2);%ith column is assigned to b variable at every step
y2 (:,2) = b;
x2 (k,:) = interp1(y3, x3, y2(:,2), 'linear');
temp=x2(k,:);
temp(isnan(temp))=0.05;
x2(k,:)=temp;
fid=fopen(['result_' num2str(2) '.txt'],'w');
fprintf(fid,'%f\n',x2);
fclose(fid);
end
end
2 件のコメント
Bob Thompson
2018 年 2 月 19 日
If I understand this right, you have created a set of code (Code 1, Code 2, etc.) that is the same thing but changes the column it references. This can be fixed with a for loop, but we'll get there. Fair warning that any time you deal with a large amount of data it is likely going to take a little while for the script to process it all. You can clean up your code so it is more neat, but that likely won't change the actual run time too much.
For your block of code that looks at each column, there are a couple of ways you can trim it down.
x2 (k,:) = interp1(y3, x3, A(:,2), 'linear'); % The use of b and y2 are not necessary at all, as you just immediately rename them without changing any actual values
x2(isnan(x2(k,:)))=0.05; % You will need to double check this one, as I'm not sure the indexing will work as smoothly, but same concept as above.
fid=fopen(['result_' num2str(2) '.txt'],'w');
fprintf(fid,'%f\n',x2); % Do you mean to write the entire x2 matrix every time you go through this loop? If you are able to write the entire matrix at once I would suggest doing so one time at the very end. Otherwise you need to index more specifically what you're trying to write out.
fclose(fid);
end
end
As for sorting through each column, it looks like you already have a for loop which does so, you just don't have the indexing set up to read it.
for i=1:size(A,2)%number of columns
In order to implement this into your code you just need to change the column references to I.
for i=1:size(A,2)
x2 (k,:) = interp1(y3, x3, A(:,i), 'linear');
x2(isnan(x2(k,:)))=0.05;
fid=fopen(['result_' num2str(i) '.txt'],'w');
fprintf(fid,'%f\n',x2(k,:));
fclose(fid);
end
回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!