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

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 know the interpolation code, but I need the selection of column and printing the interpolated data codes.
I would be grateful if someone can help me in this.
Thank you. Regards, Ismail

 採用された回答

Birdman
Birdman 2018 年 2 月 14 日

1 投票

One approach:
for i=1:size(A,2)%number of columns
b=A(:,i);%ith column is assigned to b variable at every step
interp(b,..);%you can use it in your function
end

42 件のコメント

Birdman
Birdman 2018 年 2 月 14 日
Either erase the semicolon:
for i=1:size(A,2)%number of columns
b=A(:,i)
interp(b,..);
end
or use disp:
for i=1:size(A,2)%number of columns
b=A(:,i);%ith column is assigned to b variable at every step
interp(b,..);%you can use it in your function
disp(b)
end
Ismail Qeshta
Ismail Qeshta 2018 年 2 月 14 日
Thanks Birdman. Would you please be able to suggest to me how to print the outcome out at every step?
Birdman
Birdman 2018 年 2 月 14 日
Outcome, means outcome of interp? Then:
for i=1:size(A,2)%number of columns
b=A(:,i);%ith column is assigned to b variable at every step
res=interp(b,..);%you can use it in your function
disp(res)
end
Birdman
Birdman 2018 年 2 月 14 日
I mean to print the interpolation results in a .txt file.
Answer:
for i=1:size(A,2)%number of columns
b=A(:,i);%ith column is assigned to b variable at every step
res=interp(b,..);%you can use it in your function
disp(res)
fid=fopen(['result_' num2str(i) '.txt'],'w');
fprintf(fid,'%f\n',res);
fclose(fid)
end
Ismail Qeshta
Ismail Qeshta 2018 年 2 月 14 日
Thank you very much Birdman for the code. That is really helpful. Also, I am actually extracting the data matrix from a file. I am not sure how to modify the code to suit that. Thank you again Birdman for your great help.
Walter Roberson
Walter Roberson 2018 年 2 月 14 日
Unless you have a different data matrix file for each column, you would read the data once before the loop, and then (if necessary) extract the appropriate portion of it inside the loop.
Birdman
Birdman 2018 年 2 月 14 日
You will use fscanf instead of fprintf. Imagine you want to read the file you wrote. Simply do this:
fid=fopen('result_1.txt,'r');
fscanf(fid,'%f')
fclose(fid)
The line with fscanf will display the result of the txt file.
Ismail Qeshta
Ismail Qeshta 2018 年 2 月 14 日
Many thanks Birdman. I tried to incorporate it in my code. I only get the interpolation in the first file. Would you please be able to check it for me?
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('allPiers.txt');
for i=1:size(A,37)%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
Birdman
Birdman 2018 年 2 月 14 日
i=1:size(A,37)
The above has to change to
i=1:size(A,2)
Ismail Qeshta
Ismail Qeshta 2018 年 2 月 14 日
Great. Thank you so much Birdman. The only thing is that some of the data in the .txt files are written as "NaN". Is there anyway to replace them automatically with 0.07?
Birdman
Birdman 2018 年 2 月 14 日
Replace them before writing in your for loop. So
temp=x2(k,:);
temp(isnan(temp))=0.07;
x2(k,:)=temp;
and then write to the txt file.
Ismail Qeshta
Ismail Qeshta 2018 年 2 月 14 日
Thank you Birdman. Which one exactly? I put it before the second for loop but it does not work.
Ismail Qeshta
Ismail Qeshta 2018 年 2 月 14 日
編集済み: Ismail Qeshta 2018 年 2 月 14 日
I actually mean the output "result_n.txt" files.
Birdman
Birdman 2018 年 2 月 14 日
編集済み: Birdman 2018 年 2 月 14 日
Use the following 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('allPiers.txt');
for i=1:size(A,37)%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')
temp=x2(k,:);
temp(isnan(temp))=0.07;
x2(k,:)=temp;
fid=fopen(['result_' num2str(i) '.txt'],'w');
fprintf(fid,'%f\n',x2);
fclose(fid)
end
end
Ismail Qeshta
Ismail Qeshta 2018 年 2 月 14 日
Many thanks Birdman. Shall the for i=1:size(A,37) be changed to i=1:size(A,2)?
Birdman
Birdman 2018 年 2 月 14 日
Oh yes, change it.
Ismail Qeshta
Ismail Qeshta 2018 年 2 月 14 日
I cannot thank you enough Birdman. You have been awesome. Thank you very much.
Birdman
Birdman 2018 年 2 月 14 日
You are welcome.
Ismail Qeshta
Ismail Qeshta 2018 年 2 月 14 日
Hi Birdman. Actually, after doing some further checking of the code, I found that it does not give the same results as when I use it for individual columns using the original code below. Would you please be able to help me find the error?
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 9233172.585];
x2 (k,:) = interp1(y3, x3, y2, 'linear')
end
Birdman
Birdman 2018 年 2 月 14 日
x2 (k,:)
The above refers to individual rows. You need to write it as
x2 (:,k)
Ismail Qeshta
Ismail Qeshta 2018 年 2 月 14 日
編集済み: Ismail Qeshta 2018 年 2 月 14 日
Thanks Birdman. It showed me the following error message: Subscripted assignment dimension mismatch.
Subscripted assignment dimension mismatch.
Error in Interpolate11 (line 18)
x2 (:,k) = interp1(y3, x3, y2, 'linear')
Birdman
Birdman 2018 年 2 月 14 日
Write this:
x2 (:,k) = interp1(y3, x3, y2, 'linear').'
Ismail Qeshta
Ismail Qeshta 2018 年 2 月 14 日
編集済み: Ismail Qeshta 2018 年 2 月 14 日
Thanks Birdman. I got the same error message as above. Now the code is:
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').'
temp=x2(:,k);
temp(isnan(temp))=0.07;
x2(:,k)=temp;
fid=fopen(['result_' num2str(i) '.txt'],'w');
fprintf(fid,'%f\n',x2);
fclose(fid)
end
end
Birdman
Birdman 2018 年 2 月 14 日
What does the output of
interp1(y3, x3, y2, 'linear')
look like?
Ismail Qeshta
Ismail Qeshta 2018 年 2 月 14 日
This interpolation is for getting the y values that correspond to x values from the data matrix column. It refers to the linear interpolation.
Birdman
Birdman 2018 年 2 月 14 日
What is the size(dimensions) of it?
Ismail Qeshta
Ismail Qeshta 2018 年 2 月 14 日
I don't know, but the output of x2 is 10 X 100.
Birdman
Birdman 2018 年 2 月 14 日
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];
res = interp1(y3, x3, y2, 'linear').'
x2=res(:,k);
temp=x2;
temp(isnan(temp))=0.07;
x2=temp;
fid=fopen(['result_' num2str(i) '.txt'],'w');
fprintf(fid,'%f\n',x2);
fclose(fid)
end
end
Ismail Qeshta
Ismail Qeshta 2018 年 2 月 14 日
Many thanks Birdman. I got the following error message:
Index exceeds matrix dimensions.
Error in Interpolate11 (line 19)
x2=res(:,k);
Birdman
Birdman 2018 年 2 月 14 日
Ups, I missed this. Adapt this 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 9233172.585].';
x2 (k,:) = interp1(y3, x3, y2, 'linear')
end
Ismail Qeshta
Ismail Qeshta 2018 年 2 月 14 日
編集済み: Ismail Qeshta 2018 年 2 月 14 日
Many thanks Birdman. In which part of the code should it be placed? This code is for individual data not the one we discussed above I think.
Birdman
Birdman 2018 年 2 月 14 日
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] = [8479186.526 5814080.3 7648088.256 7116870.805 9233172.585].';
x2 (:,k) = interp1(y3, x3, y2, 'linear').'
temp=x2(:,k);
temp(isnan(temp))=0.07;
x2(:,k)=temp;
fid=fopen(['result_' num2str(i) '.txt'],'w');
fprintf(fid,'%f\n',x2);
fclose(fid)
end
end
Ismail Qeshta
Ismail Qeshta 2018 年 2 月 14 日
Thanks Birdman. [y2] should take [b] values not the one inserted. Isn't it?
Ismail Qeshta
Ismail Qeshta 2018 年 2 月 14 日
I still got the same error message:
Subscripted assignment dimension mismatch.
Error in Interpolate11 (line 18)
x2 (:,k) = interp1(y3, x3, y2, 'linear').'
Birdman
Birdman 2018 年 2 月 14 日
Make sure that this y2 vector you defined manually has the same length with
b=A(:,i);
y2=b;
because that was working as I recall. Notice the lengths should be equal.
Ismail Qeshta
Ismail Qeshta 2018 年 2 月 14 日
It was actually working, but the results were unfortunately different.
Ismail Qeshta
Ismail Qeshta 2018 年 2 月 14 日
The matrix dimension problem came up only when we started changing the x2(:,k) with x2(k,:)
Birdman
Birdman 2018 年 2 月 14 日
Then keep the initial order. :)
Ismail Qeshta
Ismail Qeshta 2018 年 2 月 14 日
Thanks Birdman. Yes, that is right, but how to fix the difference in results?
Birdman
Birdman 2018 年 2 月 14 日
Maybe that should be another question in forum, right?
Ismail Qeshta
Ismail Qeshta 2018 年 2 月 14 日
Yes. I was thinking about that. Thank you very much again Birdman. I really appreciate your time and effort.
Birdman
Birdman 2018 年 2 月 14 日
You are welcome.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2018 年 2 月 14 日

コメント済み:

2018 年 2 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by