How to include 'end' in a varibable to extract a subset of the original vector ?

조회 수: 1 (최근 30일)
vector = 1:200;
%i want to extract a subset of vector
vector1 = vector(1:end-18)
%but if define a separate variable (range)
range =[1:end-18];
vector1 = vector(range);
%i got this:
Error: File: Untitled Line: 4 Column: 11
Illegal use of reserved keyword "end".
how can i define range in order to not have an error?

채택된 답변

Stephen23
Stephen23 2021년 7월 16일
"... if i want to define range before the definictiion of the vector..."
You could use an anonymous function:
rng = @(v)v(1:end-18); % range is defined!
vec = 1:200;
out = rng(vec)
out = 1×182
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Semiconductors and Converters에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by