FOR loop taking every row from a matrix

조회 수: 8 (최근 30일)
John Pickles
John Pickles 2019년 1월 7일
편집: Shubham Gupta 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
Shubham Gupta 2019년 1월 7일
편집: 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
Stephen23
Stephen23 2019년 1월 7일
For clarity it is better to specify the dimension:
size(Xin,1)
Jan
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)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by