Array instead of one value in loop form

조회 수: 3 (최근 30일)
Vivek Shukla
Vivek Shukla 2020년 10월 15일
댓글: Image Analyst 2020년 10월 16일
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
Sindar
Sindar 2020년 10월 15일
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 2020년 10월 16일
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.

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

채택된 답변

per isakson
per isakson 2020년 10월 15일
편집: per isakson 2020년 10월 15일
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

추가 답변 (0개)

카테고리

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