How to use if-else in this scenario?

조회 수: 1 (최근 30일)
Armando MAROZZI
Armando MAROZZI 2020년 11월 3일
답변: Mathieu NOE 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
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Power and Energy Systems에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by