How do I create an array with unequal increment?
조회 수: 1 (최근 30일)
이전 댓글 표시
I want to design a program to create an array of numbers. The user can input the maximum and minimum values and the scaling factor.
For example,
max=256,
min=2,
scaling factor=2.
The array will be [2 4 8 16 32 64 128 256].
Is there anyway I can create such array with the max, min and scaling factor as variables? I tried doing it with the following code. However, the x(count) only gives me 1 number instead of storing the answers from the loop.
Is this the right direction to do it? Or is there any other codes to do it in a more efficient way?
Thanks!
xmax=256;
xmin=2;
xfact=2;
count=0;
x=0;
if x<=xmax
count=count+1;
x(count)=x*xfact;
end
댓글 수: 0
채택된 답변
Star Strider
2018년 2월 14일
Try this:
expspace = @(vmin,vmax,inc) vmin * inc.^(0:( log(vmax/vmin)/log(inc)));
vmin = 2;
vmax = 256;
inc = 2;
v = expspace(vmin, vmax, inc)
v =
2 4 8 16 32 64 128 256
It is also robust to other combinations of input arguments.
댓글 수: 0
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!