Matrix Input Problem for independent numbers

2 views (last 30 days)
Hi,
Below is a preview of part of my code. I am trying to input independent numbers (in this case 1-12) of my choosing to return a matrix of [1 2 3 4 5 6 7 8 9 10 11 12]. But when I change the nf and ndof values I need the matrix to change accordingly. How can I change this so when the code runs it asks for nf*ndof number of values and then places them into a standard [n x 1] matrix for use later?
nf=input('THE TOTAL NUMBER OF FIXED NODES IN SYSTEM := ');
nnodes=nm+nf;
fix1=ndof*nf;
%fixdof
for i=1:(fix1)
str1=num2str(i);
x(i)=input(['Fixed nodes are',str1,' is := ']);
fixdof=x(i)*matrix(1,fix1,x(i));
end;
Thank you for your help.
  1 Comment
Jack
Jack on 19 Feb 2015
What I am trying to do is to define 'fixed' and 'free' nodes in a system by imputing them independently as the code is run and to put then in a (1 x n) matrix.
i.e when it prompts 'fixed nodes are' I will input 1 2 3 4 5 6 and for it to input [1 2 3 4 5 6] into the code.

Sign in to comment.

Answers (1)

Michael Haderlein
Michael Haderlein on 18 Feb 2015
What is this code supposed to do? I must admit I don't understand it. Anyway, what I see is, that fixdof will be overwritten in each loop iteration. Most likely you don't want that. So you need to use an array here, too (just as you do with x).
If I get your question right, you run your code multiple times and you enter different values for nf each time. In case nf decreases, some x won't be overwritten. Suppose nf is 4 the first time and nm is 0 and ndof is 1. x will be a 1x4 array. Now you change nf to 3, x will remain a 1x4 array and the fourth value will simply remain as it is. You know how many x you need ((nf+nm)*ndof), so just preallocate it with
x=zeros(1,(nf+nm)*ndof);
and the array will have the correct size.
You write you want the matrix to change with updated nf. You don't give the code where matrix is defined, so we cannot comment on that issue.
  2 Comments
Jack
Jack on 19 Feb 2015
Hi,
I want the user to input the nodes in a system that are 'fixed' in this case nodes 1-12 are fixed. I need this data to be independent from the last as in I could input that 1,2,3,4,5,9,10,13,15 are fixed and it would insert that into a matrix [1 2 3 4 5 9 10 13 15].
Thank you for your help.
Michael Haderlein
Michael Haderlein on 20 Feb 2015
Still not sure if I understood it. Do you mean, in the first run, the user enters 12 values. In the second run, the user enters another 12 (9, 20, whatever) values but the values of the first run should remain? In this case, I'd use a cell:
nf=input('THE TOTAL NUMBER OF FIXED NODES IN SYSTEM := ');
nnodes=nm+nf;
fix1=ndof*nf;
fixdof=zeros(1,(nf+nm)*ndof);
for i=1:(fix1)
str1=num2str(i);
x(i)=input(['Fixed nodes are',str1,' is := ']);
fixdof(i)=x(i)*matrix(1,fix1,x(i));
end;
if exist('mycell')
mycell{end+1}=fixdof;
else
mycell={fixdof};
end

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!