logaritmical spacing / incremented spacing
古いコメントを表示
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.
採用された回答
その他の回答 (1 件)
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.
カテゴリ
ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!