Command and Control vectors

조회 수: 17 (최근 30일)
Chase Lee
Chase Lee 2018년 11월 23일
답변: Guillaume 2018년 11월 23일
I am having trouble trying to make this work. I want it so that whenever a value in t is 1, it will output five zeros onto a row vector. Whenever the value of t is 2, I want it to output 5 ones to the same row vector. When the value of t is 3, I want it to output 5 twos onto the row vector. I want it so that my ending vector will be [0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 2 2 2 2 2]. Right now, it is only showing [0 1 0 1 0 2], so basically it is only outputting the value that I want once. How can I make it output the same number that I want 5 times instead of just once?
t = [1, 2, 1, 2, 1, 3];
d = 0;
for n = 1:length(t)
if (t(n) == 1)
for k = 0:4
k = k+1;
d(n) = 0;
end
elseif (t(n) == 2)
for k = 0:4
k = k+1;
d(n) = 1;
end
elseif (t(n) == 3)
for k = 0:4
k = k+1;
d(n) = 2;
end
end
end
display(d)
  댓글 수: 1
Walter Roberson
Walter Roberson 2018년 11월 23일
you can use kron or repelem

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

채택된 답변

Guillaume
Guillaume 2018년 11월 23일
A 'correct' version of your code, with all the pointless loops could be:
t = [1, 2, 1, 2, 1, 3];
d = zeros(1, numel(t)*5); %at least predeclar d with the correct size.
idx = 1; %to keep track of where we are in d
for n = 1 : numel(t)
if t(n) == 1 %note that using switch ... case statements would be more elegant than if.. elseif...
for k = 1:5
d(idx) = 0;
idx = idx + 1;
end
elseif t(n) == 2
for k = 1:5
d(idx) = 1;
idx = idx + 1;
end
elseif ((n) == 3
for k = 1:5
d(idx) = 2;
idx = idx + 1;
end
end
end
display(d)
Now of course, the k loops are completely pointless, so we could simplify that to:
t = [1, 2, 1, 2, 1, 3];
d = zeros(1, numel(t)*5); %at least predeclar d with the correct size.
idx = 1; %to keep track of where we are in d
for n = 1 : numel(t)
if t(n) == 1 %note that using switch ... case statements would be more elegant than if.. elseif...
d(idx:idx+4) = 0;
idx = idx + 5;
elseif t(n) == 2
d(idx:idx+4) = 1;
idx = idx + 5;
elseif ((n) == 3
d(idx:idx+4) = 2;
idx = idx + 5;
end
end
display(d)
Now of course, we're just repeating the same code in each of the if with the only difference being the value assigned, so they're also completely pointless:
t = [1, 2, 1, 2, 1, 3];
d = zeros(1, numel(t)*5); %at least predeclar d with the correct size.
idx = 1; %to keep track of where we are in d
for n = 1 : numel(t)
d(idx:idx+4) = t(n)-1;
idx = idx + 5;
end
display(d)
An of course, even the for loop is pointless. As per Stephen's answer you can use kron, or you can use repelem:
t = [1, 2, 1, 2, 1, 3];
d = repelem(t-1, 5)

추가 답변 (1개)

Stephen23
Stephen23 2018년 11월 23일
>> t = [1,2,1,2,1,3];
>> kron(t-1,ones(1,5))
ans =
0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 2 2 2 2 2
>>
  댓글 수: 4
Guillaume
Guillaume 2018년 11월 23일
편집: Guillaume 2018년 11월 23일
Is there any other way besides the use of kron or repelem? I would prefer just relying on a for loop within the if statement.
Yes, sure, why use one line of code when you can use 20 lines of code? You could waste even more time by adding even more lines of code to do the same thing but why?
Even ignoring the use of loops and ifs there's already plenty of pointless lines in your code, for example:
for k = 0:4
k = k + 1;
%...
end
is simply a more complicated version of
for k = 1:5
%...
end
Walter Roberson
Walter Roberson 2018년 11월 23일
d(n*5-4:n*5) = value

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

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by