Create function with 8 input and 1 output

3 ビュー (過去 30 日間)
Koran Namuq
Koran Namuq 2022 年 10 月 30 日
コメント済み: John D'Errico 2022 年 10 月 31 日
hello every one
I need to create function with 8 digital input (each input has two state one or zero ) and one output
for more explaination
if all inputs equal to 1 then out put equal to 1.5
if one input equal to 0 then output equal to 2
if two input equal to 0 then out put equal to 3
.
.
.
.
.
if 7 input equal to 0 then output equal to 8
else output equal to 0

回答 (1 件)

John D'Errico
John D'Errico 2022 年 10 月 30 日
First, dont write a function with 8 variables as input. Have ONE variable be a vector of length 8. LEARN TO USE VECTORS AND ARRAYS!
Now the result is trivial. Count the number of elements in the vector that are zero.
fun([0 0 0 0 0 0 0 0])
ans = 0
fun([1 1 1 1 1 1 0 1])
ans = 2
fun([1 1 1 1 1 1 1 1])
ans = 1.5000
fun([1 0 1 0 1 0 1 0])
ans = 5
function out = fun(V)
outReturns = [0 8 7 6 5 4 3 2 1.5];
out = outReturns(sum(V) + 1);
end
This matches your stated goal.
  2 件のコメント
Koran Namuq
Koran Namuq 2022 年 10 月 30 日
exactly I use simulink for my project so I have 8 variable input and one output
so that I should do function to connect it with my simulink project
kindly can you write full code for that in comment
thanks
John D'Errico
John D'Errico 2022 年 10 月 31 日
No. I won't write full code to do your assignment. I'm sorry, but that is your assignment. However, surely you can figure how to take 8 variables, and concatenate them into a vector? And then, could you use what I showed?

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

カテゴリ

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

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by