Can you have a if statement for even/odd?

232 ビュー (過去 30 日間)
Patrick Scott
Patrick Scott 2022 年 3 月 26 日
編集済み: Patrick Scott 2022 年 3 月 26 日
I would like to have this function check my input matrix if it is an even or odd length and then return a specific value. I'm going to write something more complex for the z_bar later on but this is the just of the function.
function [z_bar]=fnZbar(height)
if length(height)==even
z_bar= 1;
else
z_bar = 2;
end
end

採用された回答

Patrick Scott
Patrick Scott 2022 年 3 月 26 日
編集済み: Patrick Scott 2022 年 3 月 26 日
function [z_bar]=fnZbar(height)
j = length(height)/2;
k = j - fix(j);
if k==0
%Even #
z_bar= 1;
else
%Odd #
z_bar = 2;
end
end

その他の回答 (1 件)

KSSV
KSSV 2022 年 3 月 26 日
n = 1 ;
if ~mod(n,2)
fprintf('Even\n')
else
fprintf('Odd\n')
end
Odd

カテゴリ

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

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by