Specifically, I am not sure how to check if the second code reads correctly the rows and columns, particularity in the y2
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 件のコメント
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
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
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!