fill the entry of a vector with a given distance
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
mingcheng nie
am 14 Nov. 2023
Kommentiert: Dyuman Joshi
am 27 Mär. 2024
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:
- given a distance or guard space denote by d.
- the first entry must be filled.
- then the next entry will be filled at from the last filled entry.
- 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 .
- 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...
0 Kommentare
Akzeptierte Antwort
Weitere Antworten (1)
Dyuman Joshi
am 14 Nov. 2023
M = 10;
A = [1;zeros(M-1,1)];
d = 3;
A(d+1:d+1:end-(d+1))=1
2 Kommentare
Dyuman Joshi
am 14 Nov. 2023
Bearbeitet: Dyuman Joshi
am 15 Nov. 2023
@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)]
n = nnz(vec);
A = zeros(M, n);
for k=1:n
A(vec(k), k) = 1;
end
A
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!