Matrix Input Problem for independent numbers
古いコメントを表示
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 件)
Michael Haderlein
2015 年 2 月 18 日
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 件のコメント
Jack
2015 年 2 月 19 日
Michael Haderlein
2015 年 2 月 20 日
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
カテゴリ
ヘルプ センター および File Exchange で Operators and Elementary Operations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!