# FOR loop taking every row from a matrix

39 ビュー (過去 30 日間)
John Pickles 2019 年 1 月 7 日

I am working with the survey data from an Excel file, it has only three columns: [Depth, Inclination, Azimuth]. This data then is used to plot a trajectory in the subsurface.
At the momemnt, I am building a code that slightly modifies the above values of Inclination and Azimuth with simple formulas. As for now, I have a FOR loop with synthetic data of uniformly increasing Depth [1:200] and constant values of Incl & Azim, that, for each incriment in Depth (going deeper) calculates the new values of Incl. & Azimuth. Since the Incl. & Azim. values are constant, it's easy now to calculate with every new value of Depth.
Next step and problem: I have the real data of the above columns, [Depth, Inclination, Azimuth], say [X,Y,Z,] with random survey values (i.e., no uniformly increasing depth anymore. It does go deeper, but with different intervals each time; same type of change for the other two). I have imported the Excel into MATLAB and created the [3x159] matrix. I now want to incorporate the values in my FOR loop, such that it takes every row for the calculations as follows:
1. It takes X1 (Depth1), calculates in some formula the values, e.g. modification = X1+1/3;
2. Then it takes Y1, calculates some different values, e.g. Ynew = Y1 + modification;
3. Lastly, it grabs Z1, calculates another value, e.g. Znew = Z1 + modification;
And so on it repeats the same concept for X2,Y2,Z2 etc. until it reaches the end of the survey data. It will give me in the end the new matrix [X, Ynew, Znew].
Doesn't seem too complicated to tell the code to take every row of the data, but can't get it? Any help would be appreciated. Thanks!

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

### 採用された回答

Shubham Gupta 2019 年 1 月 7 日

Let's call input [3x159] matrix be Xin, and the final output [3x159] matrix be Xout. Here's is the code I would try :
Xout = zeros(size(Xin)); % predefine Xout
for i=1:size(Xin,1) % edited the dimension ( Thanks for the comments )
X = Xin(i,1);
Y = Xin(i,2);
Z = Xin(i,3);
modification = X + 1/3;
Ynew = Y + modification;
Znew = Z + modification;
Xout(i,:)=[ X, Ynew, Znew]; % save [X, Ynew, Znew] in i-th row
end
##### 3 件のコメント表示非表示 2 件の古いコメント
Jan 2019 年 1 月 7 日
@John Pickles: In for i=1:size (Xin), size replies a vector. This is equivalent to:
for i = 1:[3,159]
Now Matlab uses the first element for the colon operator only, and ignores the 159. It works, but Stephen's suggestion is celar and unique:
for i = 1:size(Xin, 1)

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

### カテゴリ

Find more on Loops and Conditional Statements in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by