I Would like to create a variable array based upon user inputs.

1 次查看(过去 30 天)
I have a variable that the user inputs called Type allocated as such:
n=input('How many measurements are you determining uncertainty upon? ');
Type=size(n)';
Uo=1;
while Uo<=n;
if Uo==1;
Type(Uo)=input('What type of measurement is your first measurement? (Input 1,2,or 3 for Pressure,Temperature, and Flow Rate Respectively) ');
Uo=Uo+1;
elseif Uo>1;
Type(Uo)=input('What type of measurement is your next measurement? (Input 1,2,or 3 for Pressure,Temperature, and Flow Rate Respectively) ');
Uo=Uo+1;
end
end
I would like to use these values to create another variable matrix later in my code. I tried the following:
Factor=cell(n:1)
for iter = 1:n
if Type == 1
Factor{iter}=1
elseif Type == 2
Factor{iter}=1
elseif Type ==3
Factor{iter}= 4
elseif Type < 1
break
elseif Type > 3
break
end
end
Could someone shine some light into what I am doing wrong here? or is there a far better method I should be using to do this?
Thanks ahead of time for any help you can lend!
  3 个评论
Adam Danz
Adam Danz 2019-1-28
Also, could you provide some examples of inputs and expected outputs?
CodeJunkie
CodeJunkie 2019-1-29
Sorry if I was a bit unclear basically the expected output I would like is something along the lines of if Type= [1,2,2,3,3,2,1] I would like to set a Variable factor to read type upon each iteration and return some factor. e.g. Factor=[5,6,6,8,8,6,5]. I was also hoping to make the factor possibly an equation so if I could keep it in matrix form that may be ideal rather than the cell form its currently in as one of the comments pointed out

请先登录,再进行评论。

采纳的回答

Jan
Jan 2019-1-29
The code contains several strange commands.
Type=size(n)'
Are you sure? Without an explanation in the comment the readers can only guess, what you want. The question "How many ..." sound like you expect a scalar input, e.g. 5. Then size(5) replies [1,1] and transposing this is [1;1].
Later you fill the elements 1 to n of Type.
Factor=cell(n:1)
Do you mean:
Factor=cell(n, 1)
n:1 is the vector from n to 1, so most likely a scalar, if n>1. Then you create a {n x n} cell matrix.
When Type is a vector, them command if Type == 3 might not do, what you expect. if requires a scalar condition, therefore an all() is inserted implicitly. I guess you mean:
if Type(iter) == 3
  1 个评论
CodeJunkie
CodeJunkie 2019-1-29
yes i meant (n,1) sorry that was a typo in the code I wrote into here. you are also correct I should specify Type per iteration it seems my issue was that simple I appreciate your help vehemently!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by