Info
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
How to prevent over-writing the y2 in my interpolation?
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi,
I would like to get the interpolation y2 for data samples using the code below. y2 is over-written at each iteration step, which renders the results incorrect. Can anyone please help me prevent that? The code is given below.
Thank you very much.
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
댓글 수: 3
Bob Thompson
2018년 2월 15일
You can turn y2 into an expanding array be indexing it.
y2(:,i) = b;
This will add a new column to y2 each time it loops through the i loop. If you would like to add a third dimension to account for the k loop then you can simply do:
y2(:,i,k) = b;
This will add the new data from b to the ith column of the kth sheet.
답변 (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!