필터 지우기
필터 지우기

fill the entry of a vector with a given distance

조회 수: 1 (최근 30일)
mingcheng nie
mingcheng nie 2023년 11월 14일
댓글: Dyuman Joshi 2024년 3월 27일
Hi there
If I have a vector A of size . I want to put some value, say 1, into some certain entries. Here are the rules:
  1. given a distance or guard space denote by d.
  2. the first entry must be filled.
  3. then the next entry will be filled at from the last filled entry.
  4. For the final filled entry, we need to check if the filled entry has enough space larger than d. For example, if and , the first place to be filled is entry 1, the second place to be filled is entry 5, and there is no the third place to be filled because for the entry 9, there is no enough space left, i.e., entry 9 and entry 10 is only has one distance, which is less than .
  5. I want to generate multiple vectors, such that each vector only has one place to be filled. For example, if $d=3 and , then for the first vector, its first place will be filled; for the second vector, its 5th place will be filled; and there is no the third vector due to the reason of rule 4.
Is there any simple code to compute this? I am a bit struggling...

채택된 답변

Matt J
Matt J 2023년 11월 15일
편집: Matt J 2023년 11월 15일
M = 10;
d = 2;
I=1:d+1:M-d;
J=1:nnz(I);
A = accumarray([I(:),J(:)],1,[M,numel(J)])
A = 10×3
1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
  댓글 수: 3
Matt J
Matt J 2024년 3월 26일
@Dyuman Joshi Very kind of you!
Dyuman Joshi
Dyuman Joshi 2024년 3월 27일
You're welcome and thanks for sharing this interesting approach!

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

추가 답변 (1개)

Dyuman Joshi
Dyuman Joshi 2023년 11월 14일
M = 10;
A = [1;zeros(M-1,1)];
d = 3;
A(d+1:d+1:end-(d+1))=1
A = 10×1
1 0 0 1 0 0 0 0 0 0
  댓글 수: 2
mingcheng nie
mingcheng nie 2023년 11월 14일
Thank you so much for your answer. I am sorry that I forget one more rule:
5. I want to generate multiple vectors, such that each vector only has one place to be filled. For example, if and , then for the first vector, its first place will be filled; for the second vector, its 5th place will be filled; and there is no the third vector due to the reason of rule 4.
Could you please update your answer for this new rule? Sorry for the inconvenience.
Dyuman Joshi
Dyuman Joshi 2023년 11월 14일
편집: Dyuman Joshi 2023년 11월 15일
@mingcheng nie, Sure, no problem.
Here, the columns of A are the vectors, which you can access them by indexing -
M = 10;
d = 3;
vec = [1 d+1:d+1:M-(d+1)]
vec = 1×2
1 4
n = nnz(vec);
A = zeros(M, n);
for k=1:n
A(vec(k), k) = 1;
end
A
A = 10×2
1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0

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

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by