How to prevent over-writing in my for loop?

1 回表示 (過去 30 日間)
Ismail Qeshta
Ismail Qeshta 2017 年 11 月 12 日
コメント済み: Ismail Qeshta 2017 年 11 月 12 日
My interpolation for loop keeps over-writing the last value, even though I assigned x2=zeros(N,2).
clear; clc;
Folder = cd;
N=100;
x2 = zeros(N, 2);
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] = [500000, 5000000, 7000000, 8000000, 900000, 13000000, 14000000];
x2 = interp1(y3, x3, y2, 'linear')
end

採用された回答

Matt J
Matt J 2017 年 11 月 12 日
編集済み: Matt J 2017 年 11 月 12 日
Your loop needs to provide an index into x2
x2(something)= interp1(y3, x3, y2, 'linear')
to tell it where to store things.
  3 件のコメント
Matt J
Matt J 2017 年 11 月 12 日
編集済み: Matt J 2017 年 11 月 12 日
Well, if you don't provide an index, MATLAB assumes that you just want to replace the whole matrix with something else:
>> x2=zeros(4,2)
x2 =
0 0
0 0
0 0
0 0
>> x2=1
x2 =
1
Conversely, if you give an index, you will place the right hand side into some designated part of the array.
>> x2=zeros(4,2); x2(4,:)=[2,1]
x2 =
0 0
0 0
0 0
2 1
So, your job is to specify where in x2 you want the result of interp1(y3, x3, y2, 'linear') inserted.
Ismail Qeshta
Ismail Qeshta 2017 年 11 月 12 日
Yes. I got it. Thanks a lot Matt.
I basically had to modify the:
x2 = zeros(N, 7);
and specify the index for x2 as per your explanation, as follows:
x2 (k,:) = interp1(y3, x3, y2, 'linear')
Thank you again for the help.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by