How to load a variable and export and edited version of it into a matrix

조회 수: 2 (최근 30일)
Basically what I have is this.
function ecef2lla = LLA (lats, longs, hts)
f = 1/298.257224;
e = 2*f - f.^2;
a = 6378137;
N = a/(1-e.^(2)*sin(lats)).^(1/2)
x = (N+hts)*cos(lats)*cos(longs);
y = (N+hts)*cos(lats)*sin(longs);
z = (N*(1-e.^2)+hts)*sin(lats);
ecef2lla = [x;y;z]
end
%%% new function, this is the actual code I'm trying to run
load ('Matrix1')
lats = Matrix1(1,1)
longs = Matrix1(1,2)
hts = Matrix1(1,3)
Matrix2 = zeros (207,3)
for L = 1:207
Matrix2(L,:) = LLA (lats(L), longs(L), hts(L))
end
I'm trying to load Matrix1 into the function and export the x,y,z edit into Matrix2. It's a 207,3 matrix.

채택된 답변

Guillaume
Guillaume 2019년 9월 21일
So, what doesn't work with your current code? It's difficult to tell you what to change if we don't know what the problem is.
One obvious problem is that you never save anything, so the export certainly doesn't happen. Since you know about load presumably you also know about save.
You haven't said what sort of file you want to create. a mat file, a text file, an excel file, something else? For that matter, you haven't told us what the input file is. It looks like it's a mat file but it could also be a text file with no extension.
One other obvious problem is that your lats, longs and hts are only one element of Matrix1, the values from the first row. So lats(L), longs(L) and hts(L) are all going to be invalid operations for any L greater than 1. Possibly you meant:
lats = Matrix1(:, 1);
longs = Matrix1(:, 2);
hts = Matrix(:, 3);
Since your data is organised by row, it would make a lot more sense if you concatenated x, y, and z horizontally instead of vertically in your LLA function:
ecef2lla = [x, y, z]; %horizontal concatenation
This would be particulalry helpful if you modified the LLA function to work with column vectors inputs instead of just scalar. For that you'd have to make sure to use dotted operators for the operations that involve vector. Ironically, most of the ones you've dotted only operate on scalar so the dot wasn't needed.
function ecef2lla = LLA (lats, longs, hts) %works with lats, longs and hts column vectors
f = 1/298.257224;
e = 2*f - f^2; %no need for .^ since f is scalar
a = 6378137;
N = a./sqrt(1-e^2*sin(lats)); %division by a vector requires ./, replaced .^(1/2) by sqrt
x = (N+hts).*cos(lats).*cos(longs); %vector multiplication .*
y = (N+hts).*cos(lats).*sin(longs);
z = (N*(1-e^2)+hts).*sin(lats); %e is scalar no need for .^, vector multiplication so .*
ecef2lla = [x, y, z]
end
with that you no longer need a loop in your other function:
Matrix2 = LLA(Matrix1(1, :), Matrix1(2, :), Matrix1(3, :));
With this you also no longer need to preallocate Matrix2 but if you had to, note that it's never a good idea to hardcode the size. If you used a files with more or less than 207 rows your code would no longer work. Much better to ask matlab how many rows your data has and use that:
Matrix2 = zeros(size(Matrix1, 1), 3); %if preallocation was required.
  댓글 수: 2
elyas levens
elyas levens 2019년 9월 22일
편집: elyas levens 2019년 9월 22일
Sorry, I'm very new to matlab and in way over my head with a new class.
As of now, with your input (which thank you so much for), the only thing that isn't working is the saving of the variable and the editing of the columns.
This is the entire code for the new test file.
Edit:Fixed the error using .*
load ('Matrix1')
f = 1/298.257224;
e = 2*f - f^2;
a = 6378137;
N = a./sqrt(1-e^2*sin(lats));
x = (N+hts).*cos(lats).*cos(longs);
y = (N+hts).*cos(lats).*sin(longs);
z = (N*(1-e^2)+hts).*sin(lats);
lats = Matrix1(:, 1).*x;
longs = Matrix1(:, 2).*y;
hts = Matrix1(:, 3).*z;
Matrix2 = zeros(size(Matrix1, 1), 3);
Matrix2 = LLA(Matrix1(1, :), Matrix1(2, :), Matrix1(3, :));
save ('Matrix2',lats,longs,hts)
Basically what I'm trying to do is take Matrix1, a 207,3 matrix of vector coordinates, multiply each column by the x,y,z respectively, and export Matrix2 of the vectors for latitude, longitude, and altitude. As far as the actual numbers go, I'm not worried about them, I just want to make the code work.
ecef2lla =
1.0e+06 *
-2.8592 -1.7970 -4.6872
1.1957 -0.3832 -0.4713
-2.4519 -6.8053 -9.7303
This is what Matrix2 is outputting as, and I want to output it out as a completely filled 207,3 rather than a 3,3. Also, the multiplication of x,y,z seems to have no effect.
The input file is a mat 207x3 double, a big matrix stored in the workspace.
Guillaume
Guillaume 2019년 9월 22일
I'm not entirely sure what code you now have. If it's exactly what you've posted where you've grouped both functions inside just one then yes, it's not going to work.
I assume that your Matrix1.mat contains a Matrix1 variable. Isn't that supposed to be your lats, longs and hts inputs? WIth your new code you're first using some unknown lats, longs, and hts (possibly leftover from previous calculations) to calculate x, y, z, then for some reason multiply these with the columns of your Matrix1 to create brand new lats, longs and hts. That's what you're later saving into Matrix2.mat. You then create a Matrix2 using the 3 columns of Matrix1 (thus completely ignoring what you've done so far) and never do anything with Matrix2.
Finally, as I (tried to) explain,
Matrix2 = zeros(size(Matrix1, 1), 3);
would be the correct way to preallocate Matrix2 if you were looping over the rows. As you're no longer looping, this is no longer needed and is actually counterproductive. You're creating a matrix of 0 which is going to be deleted and replaced on the next line.

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

추가 답변 (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