Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

How to prevent over-writing the y2 in my interpolation?

1 vue (au cours des 30 derniers jours)
Ismail Qeshta
Ismail Qeshta le 14 Fév 2018
Clôturé : MATLAB Answer Bot le 20 Août 2021
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 commentaires
Ismail Qeshta
Ismail Qeshta le 15 Fév 2018
Thank you Birdman for all your help. I really appreciate it.
Bob Thompson
Bob Thompson le 15 Fév 2018
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.

Réponses (1)

Suraj Mankulangara
Suraj Mankulangara le 20 Fév 2018
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

Community Treasure Hunt

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

Start Hunting!

Translated by