Connecting two Matlab scripts under one loop

1 回表示 (過去 30 日間)
okoth ochola
okoth ochola 2023 年 1 月 19 日
コメント済み: okoth ochola 2023 年 1 月 19 日
Hi, I written a matlab script to help me in some basic data filtration. Please see a copy of the code below. The code is for me well, however my problem is, I want to instruct the program to terminate when the first part's ccondition is not met (that is, when the percentage_S>4%), diplay the percentage and exit the operation without executing the second part which is demarcated by the comment. As at now, the two parts seems independent to each other.How can these two part be merged to dependent on each other? Such that execution of second part depend of execution of the first.You can use any random data as input if need be. Please help anyone there, thank you in advance.
A=input('enter wind speed mean\n');
%first part
a = numel(A(A<0));
b = numel(A(A>10));
S = a+b;
N = numel(A);
percentage_S = (S/N)*100;
if percentage_S<4
fprintf('Data is good proceed with analysis %g ', percentage_S)
else
end
%statrt of second part
nrows= numel(A);
ncols=1;
for i=1:nrows
for j=1:ncols
if A(i,j)<0
A(i,j)=0;
elseif A(i,j)>10
A(i,j)=10;
else
fprintf('the matrix is',A)
end
end
end
A;
percentage_S

採用された回答

VBBV
VBBV 2023 年 1 月 19 日
編集済み: VBBV 2023 年 1 月 19 日
A=100*rand(10,1); %input('enter wind speed mean\n');
%first part
a = numel(A(A<0));
b = numel(A(A>10));
S = a+b;
N = numel(A);
percentage_S = (S/N)*100
percentage_S = 80
if percentage_S>4 % simply swap the conditon and put it otherway roudn
%statrt of second part
nrows= size(A,1)
ncols=1;
for i=1:nrows
for j=1:ncols
if A(i,j)<0
A(i,j)=0;
elseif A(i,j)>10
A(i,j)=10;
else
fprintf('the matrix is %d\n the percentage is %f\n',A, percentage_S)
end
end
end
else
fprintf('Data is good proceed with analysis %g ', percentage_S)
end
nrows = 10
the matrix is 10 the percentage is 1.435338 the matrix is 1.629579e+01 the percentage is 47.914571 the matrix is 1.913592e+01 the percentage is 7.383006 the matrix is 5.963818e+01 the percentage is 81.977436 the matrix is 9.235086e+01 the percentage is 97.338002 the matrix is 80 the percentage is the matrix is 10 the percentage is 1.435338 the matrix is 10 the percentage is 10.000000 the matrix is 10 the percentage is 7.383006 the matrix is 5.963818e+01 the percentage is 81.977436 the matrix is 9.235086e+01 the percentage is 97.338002 the matrix is 80 the percentage is
  2 件のコメント
VBBV
VBBV 2023 年 1 月 19 日
編集済み: VBBV 2023 年 1 月 19 日
Swap the condition in the if-else statement, and put them inside the if part
okoth ochola
okoth ochola 2023 年 1 月 19 日
thank you, this worked

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

その他の回答 (1 件)

Jiri Hajek
Jiri Hajek 2023 年 1 月 19 日
Hi, I'm responding to your request "I want to instruct the program to terminate when the first part's ccondition is not met ". You can use the terminating command return: https://www.mathworks.com/help/matlab/ref/return.html
  1 件のコメント
okoth ochola
okoth ochola 2023 年 1 月 19 日
Wow, that was simple and fast. Thank you.

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

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

製品


リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by