Array instead of one value in loop form

1 view (last 30 days)
How can I use this code if max = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27] instad of max = 27.
Some one can please help me with this code thank you..
clear all
close all
clc
max =27;
%max=[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27]; I want to use this instead of max= 27 for same code
k=0;
s=0;
c=0;
flag=1;
state(1)=0;
% initialinput=0
% tempin=initialinput
% input= randi([0 1],1,max)
input=[ 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0];
initialinput=input(1);
output=input;
for i=1:max
if(i==1)
if(input(i)==initialinput)
state(i)=state(1);
seq(i)=s;
% s=s+1;
if(flag>1 && flag <= 9)
number=k;
integ=floor(number);
k=integ;
k=k+(2^(flag))/1000;
t(i)=k;
flag=flag+1;
else
k=k+1;
t(i)=k;
end
end
elseif(input(i)==input(i-1))
state(i)=state(i-1);
s=s+1;
seq(i)=s;
if(flag>1 && flag <= 9)
number=k;
integ=floor(number);
k=integ;
% fract=number-integ;
k=k+(2^(flag))/1000;
t(i)=k;
flag=flag+1;
else
k=k+1;
t(i)=k;
end
elseif(input(i)~=input(i-1))
state(i)=state(i-1)+1;
seq(i)=0;
s=0;
if(c==0)
flag=1;
end
number=k;
integ=floor(number);
k=integ;
k=k+1;
t(i)=k;
c=0;
%
flag=flag+1;
end
end
  2 Comments
Sindar
Sindar on 15 Oct 2020
first: "max" is an important function name, don't use it for variable names
What does it mean to use a vector for "max"? Do you want it to loop from one to each element of "max"? Or, do you want to loop over the elements of "max". If the latter, just do:
for i=not_calling_you_max
Image Analyst
Image Analyst on 16 Oct 2020
Same thing for "input" -- it's a built-in function.
I still have no idea what you want to do. Mainly because you forgot to put in crucial comments which all serious professional programmers like me put into their programs. Make it easy for us to help you not hard, by explaining what you want to do, not just overall in words, but by comments in your code.

Sign in to comment.

Accepted Answer

per isakson
per isakson on 15 Oct 2020
Edited: per isakson on 15 Oct 2020
I assume that you want to iterate over the loop for the 27 values, i=[1,2,3,...,27]. That is exactly what for i=1:27 does. (The loop for i=[1,2,3,...,27] also does that.)
The loop for i=1:[1,2,3,...,27] on the contrary runs the loop only for the value i=1. I tested on R2018b. I think Matlab should have issued a warning.
With max=27 your code produces
>> state
state =
Columns 1 through 24
0 0 0 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 3 3 3 3 4 4
Columns 25 through 27
4 4 4

More Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Tags

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!