Effacer les filtres
Effacer les filtres

How to use vectors in conditional functions?

24 vues (au cours des 30 derniers jours)
John
John le 6 Juil 2011
I have a function which accepts four numbers:
function [ force, stiff ] = myfunction( d_a, d_b, v_a, v_b)
After running my function I want to display a surf plot by putting 4 vectors through the function and then plotting.
In the function myfunction, when I introduce conditions (e.g. if, &&, ==) the function does not work. How can I get around this problem?
  4 commentaires
John
John le 22 Juil 2011
Thank you for your suggestions thus far. I have found a solution and will clarify the problem now for future people who come across this problem.
I have 4 vectors which get sent into a function. In that function the vectors run through if statements and return a single vector. This is where the error occurs:
-----??? Operands to the || and && operators must
-----be convertible to logical scalar values.
The solution I have found is using Paulo Silva's answer of idx and indexing the values where the conditions are true. This works well.
Walter Roberson
Walter Roberson le 22 Juil 2011
When you have vectors, you need to use | and & instead of || and && .
However, those will generate vectors of logical values. If you want to make decisions based upon the individual logical values, you need to loop or you need to use logical indexing. With a vector of logical values, you can use all() or any() if what you care about is the group behavior of the vector of values.

Connectez-vous pour commenter.

Réponse acceptée

Paulo Silva
Paulo Silva le 20 Juil 2011
For John's example code:
idx=a>1 & a<3 %mark the index values where your condition is true
idx1=a>2 %mark the index values where your second condition is true
c=a(idx)+a(idx1)*2
  6 commentaires
Oleg Komarov
Oleg Komarov le 20 Juil 2011
OMG I got confused.
John
John le 22 Juil 2011
Paulo Silva,
So far it this method is working, I will test it further now to see if it works under different conditions.
Thanks.

Connectez-vous pour commenter.

Plus de réponses (2)

Daniel Shub
Daniel Shub le 6 Juil 2011
Without any more information regarding what doesn't work, I am just guessing ...
The == operator with a scalar returns a scalar, with an array it returns an array. You may want to replace
x == y
with
any(x == y)
or
all(x == y)
you may also want to replace && with just & and with |.

John
John le 20 Juil 2011
Thank you for your replies. I have created a sample code to illustrate what I am doing.
a = 3; % Say, a is a vector
if a>1 && a<3
b=1;
elseif a>2
b=2;
end
c=a*b; % Say, c is the resulting vector
% with the relevant 'b' for each number in vector
I realise I can do the same thing with a for loop, however, I think there is a much easier method.
  1 commentaire
Walter Roberson
Walter Roberson le 20 Juil 2011
John, if "a" is in the range of being greater than 2 and less than 3, then that satisfies a>1 & a<3, so b=1 would be appropriate for that case according to your code. But then you have the "elseif" for a>2 : are you sure that for the range (2,3) you want b=1 and not the b=2 of the elseif ? Otherwise you might as well (for the vector case) use
if a>1 && a<3
b = 1;
elseif a>=3
b = 2;
end

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by