How would I replace a string of numbers with more numbers using for and if statements?

조회 수: 2 (최근 30일)
Here is my code so far. I'm trying to make it so that if the number is "1" in the string, it replaces it with five zeros. If the number is "2" in the string it replaces that with 5 ones, and if the number is "3" in the string, it replaces it with 5 twos and make that all a different string of numbers called d. The end should look like this: d = [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]. I really need help, I am awful with matlab.
if true
t = [1,2,1,2,1,3];
% For Loop
for n = 1:length(t)
if t(n) == 1
d(n) = 0,0,0,0,0;
elseif t(n) == 2
d(n) = 1,1,1,1,1;
else t(n) == 3
d = 2,2,2,2,2;
end
end
end

채택된 답변

madhan ravi
madhan ravi 2018년 11월 3일
편집: madhan ravi 2018년 11월 3일
t = string([1,2,1,2,1,3]);
d=cell(1,numel(t)); %pre-allocation
for i = 1:numel(t)
if t(i)=='1'
d{i}=[ones(1,5)*0];
elseif t(i)=='2'
d{i}=[ones(1,5)*1];
elseif t(i)=='3'
d{i}=[ones(1,5)*2];
end
end
d = horzcat(d{:})
command window:
>> COMMUNITY
d =
Columns 1 through 13
0 0 0 0 0 1 1 1 1 1 0 0 0
Columns 14 through 26
0 0 1 1 1 1 1 0 0 0 0 0 2
Columns 27 through 30
2 2 2 2
>>
  댓글 수: 2
Jacob Roach
Jacob Roach 2018년 11월 3일
편집: Jacob Roach 2018년 11월 3일
Wow, I didn't even know about the horzcat command, do you think you would be able to explain your thought process or something? I know this is a simple matlab code for you probably, but it's so frustrating not being able to understand why you're doing what you do. Thank you so much!
madhan ravi
madhan ravi 2018년 11월 3일
1)just created 1 by 5 cell array;
2) made a loop until the number of cell array;
3)made if and else if conditions to store elements in each cell when the condition is satisfied;
4)finally concatenated them horizontally.
What's the advantage?
Cells are huge containers they can compress how much ever you feed inside them unlike the normal arrays

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

추가 답변 (1개)

Tyler Johns
Tyler Johns 2018년 11월 3일
Keep in mind when programming there is usually more than one way to accomplish your goal. Here is an additional solution. This may not be the most efficient way since the size of vector d changes every loop iteration but it is still a solution nonetheless.
t = [1 2 1 2 1 3];
d=[]; % create empty vector d
for n = 1:length(t)
if t(n)==1
d = [d 0 0 0 0 0]; %add new numbers to end of vector d
elseif t(n)==2
d = [d 1 1 1 1 1];
elseif t(n)==3
d = [d 2 2 2 2 2];
end
end
disp(d)
  댓글 수: 4
Jacob Roach
Jacob Roach 2018년 11월 3일
Alright, so basically I need to make a "random walker" that walks in either direction using a for loop. It can walk forward one, back one back one forward one, it just has to be random for a thousand times. Then I need to use comet to simulate the walk, and im just not sure how to set up this code. So far I have this. I need to plot the walk as a function of time. I don't know what I'm doing wrong.
%%Part 1
% Simulation of Walker
t = linspace(0,1001,50000);
x = 0;
d = randn(0,2);
for n = 1:1001
if d == 0
x = x;
elseif d == 1
x = x + 1;
else d == 2
x = x - 1;
end
end
comet(x,t);
Stephen23
Stephen23 2018년 11월 3일
Follow madhan ravi's answer: it shows good practice: a preallocated cell array to store the intermediate arrays.

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by