Fill a zeros matrix with another matrix until it is full
조회 수: 6 (최근 30일)
이전 댓글 표시
I want to fill the array essai with the value in the array key but my code return zeros
k= 1:length(key);
yr=reshape(y.',1,[]);
essai=zeros(1,length(yr));
essai=uint8(essai);
for n= 1:length(essai)
if k <length(key)
essai(n)=essai(n)+key(k)
else if k== length(key)
essai(n)=essai(n)+key(k);
k=1;
end
end
end
댓글 수: 2
Sumara
2019년 6월 14일
THANK YOU! I've wanted to do this for when my code became misaligned but didn't know there was a command for it and fixing manually is so tedious !!!
채택된 답변
Stephen23
2018년 2월 5일
편집: Stephen23
2018년 2월 5일
MATLAB is not an ugly low-level language like C++ and does not need loops to solve all tasks:
idx = 1+mod(0:numel(y)-1,numel(key));
essai = uint8(key(idx));
And tested on some random data:
>> y = 0:9;
>> key = 2:2:8;
>> idx = 1+mod(0:numel(y)-1,numel(key));
>> essai = uint8(key(idx))
essai =
2 4 6 8 2 4 6 8 2 4
댓글 수: 3
Stephen23
2018년 2월 5일
편집: Stephen23
2018년 2월 5일
"but can you explain this idx thing please"
idx is a vector of indices. Lets look at my example data:
>> y = 0:9 % a vector with ten elements.
y =
0 1 2 3 4 5 6 7 8 9
>> key = 2:2:8 % a vector with four elements.
key =
2 4 6 8
>> idx = 1+mod(0:numel(y)-1,numel(key)) % index vector
idx =
1 2 3 4 1 2 3 4 1 2
>> key(idx) % use the indices
ans =
2 4 6 8 2 4 6 8 2 4
You can see how the idx values are 1 to 4 repeated up until the vector has the same number of indices as y has elements: these indices determine which elements of key will get selected: so the output vector is equivalent to
[key(1),key(2),key(3),key(4),key(1),key(2),key(3),...]
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!