Move value at index i from one array to another array but at index 3 * i

조회 수: 4 (최근 30일)
SAMUEL DAVID BERENSOHN
SAMUEL DAVID BERENSOHN 2021년 3월 16일
답변: Stephen23 2021년 3월 16일
I am trying to take an array [1 2 3] and scale the indices of each of the elements by three. So, as a result, I am hoping to have [1 0 0 2 0 0 3]. So far, I am using a for loop:
ytemp = zeros(length(input), 1);
for idx = 1:length(y_fft)
ytemp(idx * 3) = y_fft(idx);
end
Is there a better way to do this?

답변 (2개)

Mohammad Sami
Mohammad Sami 2021년 3월 16일
편집: Mohammad Sami 2021년 3월 16일
You can also do it this way.
a = [1 2 3];
b = [a; zeros(2,size(a,2))];
c = reshape(b,1,[]);
c(end-1:end) = [];
or as follows
a = [1 2 3];
c(1:3:(3*length(a))) = a

Stephen23
Stephen23 2021년 3월 16일
Robust and reasonably efficient:
n = 3; % scale
a = 1:3
a = 1×3
1 2 3
a(2:n,:) = 0;
a = a(1:1-n+end)
a = 1×7
1 0 0 2 0 0 3

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by