To calculate the end point.

3 ビュー (過去 30 日間)
Odien
Odien 2015 年 7 月 31 日
回答済み: Image Analyst 2015 年 7 月 31 日
TO generate array, we can use this code A(1:2:10) which means to create a array with equally spaced with size 2 , start point is 1 and the end point is 10.
The question is, if we wan 20 equally spaced element in an array, size can be any. How to let the Matlab to calculate the end point for us? For example, A(5:6: n ) , i wan a array start with 5 then equally space with size 6 and i wan 20 elements. How to let Matlab find the n ?

回答 (2 件)

Star Strider
Star Strider 2015 年 7 月 31 日
This works:
A = 5:6:(19*6+5);
  2 件のコメント
Odien
Odien 2015 年 7 月 31 日
instead of giving formula ( which consider we calculate ourself) , is there any special code can use?
Star Strider
Star Strider 2015 年 7 月 31 日
This is the code for the calculation:
start = 5;
interval = 6;
len = 20;
A = start:interval:((len-1)*interval+start);

サインインしてコメントする。


Image Analyst
Image Analyst 2015 年 7 月 31 日
He said "A(1:2:10)" so I thought he's looking to extract those elements of A into another array, not to create an A with elements having those values . So I think he's wanting this:
% Generate sample data in A:
A = randi([0, 9], 1, 130)
% Define extraction parameters:
startIndex = 5;
interval = 6;
numSamples = 20; % How many elements we want to extract from A.
% Figure out what the last index will be:
lastIndex = startIndex + (numSamples - 1) * interval
% Extract numSamples from A at the specified start index and spacing:
B = A(startIndex : interval : lastIndex)
% Let's check the length to be sure it's 20:
fprintf('The length of B is %d elements.\n', length(B));

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by