# How to extract values from an array?

조회 수: 167(최근 30일)
Ishita Agrawal 2020년 3월 10일
답변: Stijn Haenen 2020년 3월 10일
Hello all,
I am trying to extract values from an array using the command given below.
x = 1:0.1:2;
val1 = x(1:4);
val2 = x(1):x(4);
Can anyone tell how val1 is different than val2?

댓글을 달려면 로그인하십시오.

### 답변(2개)

Star Strider 2020년 3월 10일
The default ‘step’ for the colon operator is 1. So ‘val1’ correctly addresses elements 1 through 4.
Since ‘x’ has values from 1 to 2 with a step of 0.1, and since ‘x(1)’ is 1 and ‘x(4)’ is 1.3, the next value after ‘x(1)’ is 2. Since this is greater than 1.3, ‘val2’ returns only the first value in the series, that being ‘x(1)’ or 1.
##### 댓글 수: 0표시숨기기 이전 댓글 수: -1

댓글을 달려면 로그인하십시오.

Stijn Haenen 2020년 3월 10일
val1 contains the numbers x(1), x(2) x(3) and x(4). val2 contains the numbers between x(1) and x(4) with steps on 1, but x(4) is not large enoug to complete the step so only x(1) is in the list

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!