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]

采纳的回答

VBBV
VBBV 2022-10-25
data=load('artificial_slope.txt') %loading data
data = 8×6
-0.7880 0.5238 -0.5724 -0.6947 -0.5819 0.4228 -0.3967 0.8456 0.5138 -0.6947 -0.5819 0.4228 -1.0000 1.3560 0.2264 -0.6947 -0.5819 0.4228 -1.1571 0.9290 -0.6202 -0.6947 -0.5819 0.4228 1.0021 0.8320 1.0846 0.0989 -0.1012 0.9899 1.9014 0.9879 1.0091 0.0989 -0.1012 0.9899 2.7729 0.7271 0.8938 0.0989 -0.1012 0.9899 1.4571 1.5036 1.1069 0.0989 -0.1012 0.9899
% 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
disc_plane = 8×5
180.6973 64.9896 -0.6947 -0.5819 0.4228 180.6973 64.9896 -0.6947 -0.5819 0.4228 180.6973 64.9896 -0.6947 -0.5819 0.4228 180.6973 64.9896 -0.6947 -0.5819 0.4228 180.7970 8.1340 0.0989 -0.1012 0.9899 180.7970 8.1340 0.0989 -0.1012 0.9899 180.7970 8.1340 0.0989 -0.1012 0.9899 180.7970 8.1340 0.0989 -0.1012 0.9899
Check the spelling of variable dipdir as against dip_dir
  2 个评论
VBBV
VBBV 2022-10-25
编辑:VBBV 2022-10-25
you have incorrect spelling in the line
disc_plane = [dip_dir, dip_disc, nx, ny, nz]
in place of
disc_plane = [dipdir, dip_disc, nx, ny, nz]
Hrvoje Lukacic
Hrvoje Lukacic 2022-10-25
Thank you for your help, the core works. The key was in the for loop

请先登录,再进行评论。

更多回答(1 个)

Davide Masiello
Davide Masiello 2022-10-25
编辑:Davide Masiello 2022-10-25
data=load('artificial_slope_2.txt') %loading data
data = 8×6
-0.7880 0.5238 -0.5724 -0.6947 -0.5819 0.4228 -0.3967 0.8456 0.5138 -0.6947 -0.5819 0.4228 -1.0000 1.3560 0.2264 -0.6947 -0.5819 0.4228 -1.1571 0.9290 -0.6202 -0.6947 -0.5819 0.4228 1.0021 0.8320 1.0846 0.0989 -0.1012 0.9899 1.9014 0.9879 1.0091 0.0989 -0.1012 0.9899 2.7729 0.7271 0.8938 0.0989 -0.1012 0.9899 1.4571 1.5036 1.1069 0.0989 -0.1012 0.9899
% 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)
-0.6947 -0.5819 0.4228 -0.6947 -0.5819 0.4228 -0.6947 -0.5819 0.4228 -0.6947 -0.5819 0.4228 0.0989 -0.1012 0.9899 0.0989 -0.1012 0.9899 0.0989 -0.1012 0.9899 0.0989 -0.1012 0.9899
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
ans = 8×1 logical array
0 0 0 0 1 1 1 1
n(:,1) > 0
ans = 8×1 logical array
0 0 0 0 1 1 1 1
n(:,1) <= 0
ans = 8×1 logical array
1 1 1 1 0 0 0 0
n(:,1) < 0
ans = 8×1 logical array
1 1 1 1 0 0 0 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]
cond = 8×4 logical array
0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 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)
dip_dir = 8×1
180.7970 180.7970 180.7970 180.7970 80.6973 80.6973 80.6973 80.6973
disc_plane = [dip_dir, dip_disc, nx, ny, nz]
disc_plane = 8×5
180.7970 64.9896 -0.6947 -0.5819 0.4228 180.7970 64.9896 -0.6947 -0.5819 0.4228 180.7970 64.9896 -0.6947 -0.5819 0.4228 180.7970 64.9896 -0.6947 -0.5819 0.4228 80.6973 8.1340 0.0989 -0.1012 0.9899 80.6973 8.1340 0.0989 -0.1012 0.9899 80.6973 8.1340 0.0989 -0.1012 0.9899 80.6973 8.1340 0.0989 -0.1012 0.9899
Hope this helps you.
  6 个评论
Davide Masiello
Davide Masiello 2022-10-25
What formula should be used to obtain the value of 230?
Hrvoje Lukacic
Hrvoje Lukacic 2022-10-25
Sorry values are correct. The code written by VBBV is correct. Thank you for your help

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by