logaritmical spacing / incremented spacing

조회 수: 2 (최근 30일)
Örs Istok
Örs Istok 2019년 7월 9일
댓글: Örs Istok 2019년 7월 9일
Dear all,
I wish to write a code where I have two values, 0 and 1. Lets say I have a 500x1 array called A.
The code should be distributing the values of 1 logaritmically spaced in the array A. Where there is no 1 the program would put 0.
Something like
A=[1; 0; 1; 0; 1;0;0;1;0;0;1;.....0;0;0;0;0;0;1;0;0;0;0;0;0;1;.......0;0;0;0;0;0;0;0;0;0;1;...A(xn,1)]; where xn = 500.
I know what I did is a logaritimical spacing, its more like an incremented spacing;
My question would be, which would be the easiest way to achieve this. I made something but it needs a prebuilt CSV file which containts the positions of 1 and 0. But to do this for 500 numbers and to consistently increase the spacing it can take some time.
I hope this makes sense, if not please let me know and I will try my best to clarify what I mean :).
Thank you all in advance for your time and help.
Best wishes,
Ors.

채택된 답변

Stephen23
Stephen23 2019년 7월 9일
>> diff(fix(log2((1:32).')))
ans =
1
0
1
0
0
0
1
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
  댓글 수: 3
Guillaume
Guillaume 2019년 7월 9일
Well, you haven't really defined what logarithmic spacing is. Stephen's answer gives you spacing in powers of 2. You can use any other power but you'll have to use fractional powers (smaller than 2) to get more frequent 1s.
diff(fix(log(1:500)/log(1.5))) > 0
Örs Istok
Örs Istok 2019년 7월 9일
Yes you are right. Honestly I am not that advanced with maths. Thank you this sorted my problem. :)

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

추가 답변 (1개)

Steven Lord
Steven Lord 2019년 7월 9일
Look at the spacing of the 1's in your vector. It looks like the second is 2 elements after the first, the third is 2 elements after the second, the fourth and fifth are 3 elements after the third and fourth respectively, etc. So if we take the spacing vector:
spacing = repelem(2:5, 1, 2)
add a 1 to the front as the location of the first 1:
spacing = [1 spacing]
and now take the cumulative sum (the third 1 is 2 elements after the second which is 2 elements after the first, so the third is at location 1 + 2 + 2) we have the indices of elements that should be set to 1.
locations = cumsum(spacing)
Now use those as indices.
z = zeros(1, max(locations));
z(locations) = 1
It is possible to do this more compactly, but seeing the steps laid out should help you see why this works more easily.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by