Thread Subject: How to use inline logic check

Subject: How to use inline logic check

From: ttkingdom

Date: 26 Nov, 2009 08:05:41

Message: 1 of 3

I'm very new to Matlab. I just found out from some online M-files that
a logic check can be add like this
   % Calculate all divisor of n
   d = 1 : (n / 2);
   q = n ./ d;
   d = d(q == round(q)); % <--- this is it
   d(end + 1) = n;

So there is no need for if .. end. Please explain to me how this work
exactly (or point me to a Matlab help page, cause I can only find the
page on logical operator)

Subject: How to use inline logic check

From: Jos (10584)

Date: 26 Nov, 2009 08:49:02

Message: 2 of 3

ttkingdom <ttkingdom@gmail.com> wrote in message <75a41474-c4e9-4e75-818d-a07625c1eaa6@w19g2000pre.googlegroups.com>...
> I'm very new to Matlab. I just found out from some online M-files that
> a logic check can be add like this
> % Calculate all divisor of n
> d = 1 : (n / 2);
> q = n ./ d;
> d = d(q == round(q)); % <--- this is it
> d(end + 1) = n;
>
> So there is no need for if .. end. Please explain to me how this work
> exactly (or point me to a Matlab help page, cause I can only find the
> page on logical operator)

This is called logical indexing. Here is a short example

% some data
  A = [10 20 30 40 50 60]
  x = [1 2 1 1 2 2]

% goal: find elements in A where the correponding location of x equals 2
logvec = x == 2 % logical vector (true/false)
B = A(logvec) % retrieve elements of A where logvec is true

% two detours, only useful if you want to know the positions where x equals 2 as well
  indexnum = find(logvec)
  B2 = A(indexnum)
% detour 2
  indexnum2 = 1:numel(A) ;
  indexnum2 = indexnum2(logvec)
  B3 = A(indexnum2)

and here is some help
http://www.mathworks.com/access/helpdesk/help/techdoc/math/f1-85462.html

hth
Jos

Subject: How to use inline logic check

From: ttkingdom

Date: 26 Nov, 2009 09:46:19

Message: 3 of 3

thank you very much.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
logical indexing Jos (10584) 26 Nov, 2009 03:54:05
rssFeed for this Thread

Contact us at files@mathworks.com