9

H0 is an array ([1:10]), and H is a single number (5).

How to compare every element in H0 with the single number H?

For example in

if H0>H 
   do something
else
   do another thing
end

MATLAB always does the other thing.

3 Answers 3

9

if requires the following statement to evaluate to a scalar true/false. If the statement is an array, the behaviour is equivalent to wrapping it in all(..).

If your comparison results in a logical array, such as

H0  = 1:10;
H   = 5;
test = H0>H;

you have two options to pass test through the if-statement:

(1) You can aggregate the output of test, for example you want the if-clause to be executed when any or all of the elements in test are true, e.g.

if any(test)
  do something
end

(2) You iterate through the elements of test, and react accordingly

for ii = 1:length(test)
   if test(ii)
      do something
   end
end

Note that it may be possible to vectorize this operation by using the logical vector test as index.

edit

If, as indicated in a comment, you want P(i)=H0(i)^3 if H0(i)<H, and otherwise P(i)=H0(i)^2, you simply write

 P = H0 .^ (H0<H + 2)
Sign up to request clarification or add additional context in comments.

3 Comments

I used 'for' but there was a problem for ii = 1:length(test) if test(ii) P=H0 end end because H0 is a an array of 10 numbers I got 10*10 values of P not just 10 I know I'm so beginner but any way thank you
@Nawar: you have to index properly, for example P(ii)=H0(ii). However,if you want to apply the function you mentioned in the other comment, then my edit will allow you to do this in one simple line.
Can you please put up the last solution to the top of your answer?
2

@Jonas's nice answer at his last line motivated me to come up with a version using logical indexing.

Instead of

for i=1:N
  if H0(i)>H
     H0(i)=H0(i)^2;
  else
     H0(i)=H0(i)^3;
  end
end

you can do this

P = zeros(size(H0)); % preallocate output
test = H0>H;

P(test) = H0(test).^2; % element-wise operations 
                       % on the elements for which the test is true

P(~test) = H0(~test).^3; % element-wise operations 
                         % on the elements for which the test is false

Note that this is a general solution.

Comments

1

Anyway take a look at this: using ismemeber() function. Frankly not sure how do you expect to compare. Either greater than, smaller , equal or within as a member. So my answer might not be yet satisfying to you. But just giving you an idea anyway.

H0 = [0 2 4 6 8 10 12 14 16 18 20];
H  = [10];
ismember(H,H0)
IF (ans = 1) then
// true
else
//false
end 

Update Answer

This is super bruteforce method - just use it explain. You are better off with any other answers given here than what I present. Ideally what you need is to rip off greater/lower values into two different vectors with ^3 processing - I assume... :)

H0 = [0 2 4 6 8 10 12 14 16 18 20];
H  = [10];

H0(:)
ans = 
     0
     2
     4
     6
     8
     10
     12
     14
     16
     18
     20

Function find returns indices of all values in H0 greater than 10 values in a linear index.

X = find(H0>H)
X = 
     7
     8
     9
     10
     11

Function find returns indices of all values in H0 lower than 10 in a linear index.

Y = find(H0<H)
Y =
     1
     2
     3
     4
     5
     6

If you want you can access each element of H0 to check greater/lower values or you can use the above matrices with indices to rip the values off H0 into two different arrays with the arithmetic operations.

G = zeros(size(X)); // matrix with the size = number of values greater than H
J = zeros(size(Y)); // matrix with the size = number of values lower than H

for i = 1:numel(X)
     G(i) = H0(X(i)).^3
end

G(:)
ans =

      1728
      2744
      4096
      5832
      8000

for i = 1:numel(Y)
     J(i) = H0(Y(i)).^2
end

J(:)
ans =

      0
      4
     16
     36
     64
    100

1 Comment

I want to compare every element on the array is less than the number or otherwise so if H0<H P=H0^3, else P=H0^2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.