How to select columns from data matrix using "for" loop?

4 次查看(过去 30 天)
Hi,
I would like to select columns from a data matrix and use them to interpolate data from another file. For example, first column is selected and interpolated from another file, and printed, then the second column, and so on.
I have modified the first interpolation code to make it suitable for the data matrix column selection and provided the second code, as shown below. The problem is that the second code does not provide similar data. Can anyone please help me figure the out the error in the code? Thank you very much.
Original code:
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);
[y2] = [8479186.526 5814080.3 7648088.256 7116870.805];
x2 (k,:) = interp1(y3, x3, y2, 'linear')
end
Second code:
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=importdata('AllPiers2.txt');
for i=1:size(A,2)%number of columns
b=A(:,i);%ith column is assigned to b variable at every step
y2 = b;
x2 (k,:) = interp1(y3, x3, y2, 'linear');
fid=fopen(['result_' num2str(i) '.txt'],'w');
fprintf(fid,'%f\n',x2);
fclose(fid);
end
end
  7 个评论
Ismail Qeshta
Ismail Qeshta 2018-2-15
Hi Bob. Many thanks for your comment. To make my question clearer, I am explaining it as follows:
I have 100 plots and one file that contains 40 columns. I need to use each column data in this file to be interpolated in those 100 plot.
The steps are as follows:
1) Plot z1 = x1,y1 data; Interpolate the first column data from z1
2) 1) 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 40th column.
I have built the above code, but it does not correctly give the results. I think there is something incorrect in the "for" loop. Would you please be able to help me in correcting it?
Bob Thompson
Bob Thompson 2018-2-15
Aside from the fact that y2 is a row vector in the first code and a column vector in the second code, I do not see anything specifically wrong with the code.
It is worth mentioning that the outputs of "results_i.txt" are going to be only columns 1-40 for plot 100. Because the column for loop is inside the plot for loop you are evaluating plot one, then all 40 columns, THEN moving on to plot two and all 40 columns. You are also overwriting the results files each time you do a new plot, because the naming convention doesn't involve any number for the plot designation. So, maybe it's not a "wrong" answer, just an unexpected one?

请先登录,再进行评论。

回答(1 个)

Suraj Mankulangara
Suraj Mankulangara 2018-2-20
Hello Ismail,
I understand that you have 100 plots, and a data file containing 40 columns, and that in each plot, you want to interpolate data from each of the 40 columns from the data file.
Here are a few things you might want to try out:
1) Ensure that the variable 'y2' has the correct dimensions. In Code 1, y2 is a row vector, and in Code 2, y2 appears to be a column vector. The size of the result of the call to interpl() function depends on the dimensions of y2. If this is not proper, then the assignment
x2 (k,:) = interp1(y3, x3, y2, 'linear');
might result in a "Subscripted assignment dimension mismatch" error.
2) The line
A=importdata('AllPiers2.txt');
Can be placed outside the outer for loop, since the data only needs to be imported once.
3) If, for each of the 100 plots, you want to interpolate data from each of the 40 columns, you would need 100*40 result files. You might want to store them in the format "result_k_i " where 'k' is the outer for loop variable and 'i' is the inner for loop variable. Otherwise, the results will get overwritten in each iteration of the outer for loop.
Sincerely
Suraj Mankulangara

类别

Help CenterFile Exchange 中查找有关 Interpolation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by