# How to use if-else in this scenario?

3 ビュー (過去 30 日間)
Armando MAROZZI 2020 年 11 月 3 日

I have a vector of numbers. What I would like to do is to change the composition of this vector depending on which function I am using.
Let me explain it in more details:
% this is the vector I have
t = [1; 1; 1; 5; 5; 5; 2; 5; 5]
% Now let's assume I have two functions x and y that take t as an argument.
% I would like to write a code that says t equals to the vector you see above in function x
% while for function y every element = 1 in t becomes 6
Can anyone help me do it?
Thanks!

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

### 採用された回答

Mathieu NOE 2020 年 11 月 3 日
hello Armando
I don't see what the first function is supposed to do ? output = input ? so what ?
for the second task, this should do the trick :
t = [1; 1; 1; 5; 5; 5; 2; 5; 5]
val1 = 1; % value to be replaced in t
val2 = 6; % value in replacement
output = Y(t)
% second function Y
function output = Y(input,val1,val2)
output = input;
ind = find(output==val1);
output(ind) = val2;
end

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

### カテゴリ

Help Center および File ExchangePower and Energy Systems についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by