IF statement not generating results?
2 次查看(过去 30 天)
显示 更早的评论
Hello! I wrote a code for calculation of some geological parameters. The problem is that the code is not generating parameter dipdir which should be calculated using IF statement.
Can some point me in the right direction for solving this problem?
(I am attaching small part of the data set for testing.
Thank you all in advance
data=load('artificial_slope.txt') %loading data
% defining input values based of input txt file
nx = data(:,4);
ny = data(:,5);
nz = data(:,6);
n = [nx,ny,nz];
n = n./vecnorm(n,2,2); % normalize to unit-vectors. People promise to only give you the unit-length
% normal-vector, if you normalize it you're sure it is. Big
% difference. Big.
cosA = n(:,1);
cosB = n(:,2);
cosG= n(:,3);
% dip calculation
dip_disc = acos(cosG)*(180/pi);
if n(:,1) >= 0 & n(:,2) >= 0
dipdir = atan(n(:,2)./n(:,1));
elseif n(:,1) > 0 & n(:,2) < 0
dipdir = 180 - atan(n(:,2)./n(:,1));
elseif n(:,1) <= 0 & n(:,2) <= 0
dipdir = 180 + atan(n(:,2)./n(:,1));
elseif n(:,1) < 0 & n(:,2) > 0
dipdir = 360 - atan(n(:,2)./n(:,1));
end
disc_plane = [dip_dir, dip_disc, nx, ny, nz]
0 个评论
采纳的回答
VBBV
2022-10-25
data=load('artificial_slope.txt') %loading data
% defining input values based of input txt file
nx = data(:,4);
ny = data(:,5);
nz = data(:,6);
n = [nx,ny,nz];
n = n./vecnorm(n,2,2); % normalize to unit-vectors. People promise to only give you the unit-length
% normal-vector, if you normalize it you're sure it is. Big
% difference. Big.
cosA = n(:,1);
cosB = n(:,2);
cosG= n(:,3);
% dip calculation
dip_disc = acos(cosG)*(180/pi);
dipdir = zeros(8,1);
for k = 1:length(n)
if n(k,1) >= 0 & n(k,2) >= 0
dipdir(k) = atan(n(k,2)/n(k,1));
elseif n(k,1) > 0 & n(k,2) < 0
dipdir(k) = 180 - atan(n(k,2)/n(k,1));
elseif n(k,1) <= 0 & n(k,2) <= 0
dipdir(k) = 180 + atan(n(k,2)/n(k,1));
elseif n(k,1) < 0 & n(k,2) > 0
dipdir(k) = 360 - atan(n(k,2)/n(k,1));
end
end
disc_plane = [dipdir, dip_disc, nx, ny, nz] % check the spelling of variable dipdir
Check the spelling of variable dipdir as against dip_dir
更多回答(1 个)
Davide Masiello
2022-10-25
编辑:Davide Masiello
2022-10-25
data=load('artificial_slope_2.txt') %loading data
% defining input values based of input txt file
nx = data(:,4);
ny = data(:,5);
nz = data(:,6);
n = [nx,ny,nz];
n = n./vecnorm(n,2,2); % normalize to unit-vectors. People promise to only give you the unit-length
% normal-vector, if you normalize it you're sure it is. Big
% difference. Big.
cosA = n(:,1);
cosB = n(:,2);
cosG= n(:,3);
% dip calculation
dip_disc = acos(cosG)*(180/pi);
disp(n)
The problem as I see it is that you are passing vectors to the conditional statement.
In order for the command to be executed, you need the condition to be true for all elements of the vector.
The very first instruction in each statement, i.e.
n(:,1) >= 0
n(:,1) > 0
n(:,1) <= 0
n(:,1) < 0
is never verified for all elements of the first column of n, therefore none of the commands in the conditional statement are executed and dip_dir is not defined in the first place.
You can probably get away without conditional statement.
If you put your four conditions in a matrix, you will see that the conditions being fulfilled are number 3 for the first 4 rows and number 2 for the second 4 rows.
cond = [n(:,1) >= 0 & n(:,2) >= 0,n(:,1) > 0 & n(:,2) < 0,n(:,1) <= 0 & n(:,2) <= 0,n(:,1) < 0 & n(:,2) > 0]
You can also calculate all the possible occurences for dip_dir
dip_dir0 = [atan(n(:,2)./n(:,1)),180 - atan(n(:,2)./n(:,1)),80 + atan(n(:,2)./n(:,1)),360 - atan(n(:,2)./n(:,1))];
And finally get only those which fulfill the conditions given by the matrix cond.
dip_dir = dip_dir0(cond)
disc_plane = [dip_dir, dip_disc, nx, ny, nz]
Hope this helps you.
6 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!