How to take x as a vector in for loop?

9 次查看(过去 30 天)
NN=zeros(nx+1,nx+1);
xx=0.998;
for i=1:nx+1
count=1;
for x=dx:length: nx+1
if(i-1)*xx<=x<=i*xx
i;
NN(i,count)=(x-(i-1)*dx)/dx;
elseif i*xx<=x<=(i+1)*xx
i;
NN(i,count)=((i+1)*dx-x)/dx;
end
NN(NN<0)=0;
count=count+1;
end
end
This code shows dimension error in
T_check = interp1(xts,pts,x,'linear');
because x is showing only one value here. Anyone please guide me how to correct x?
  1 个评论
Stephen23
Stephen23 2017-9-7
@Qudsiya Irum: your code is very badly indented. Bad code alignment is a common way that beginners hide errors in their code. Fixing code indentation is easy: click ctrl+i in the MATLAB editor.

请先登录,再进行评论。

采纳的回答

Eric
Eric 2017-9-7
The variable x here is a for loop counter, which means it will iterate through the values given by dx:length:nx+1 and therefore only ever have one value at a time. If you want x to be the entire vector, set x=dx:length:nx+1 again on its own after the for loop.
Protip: You can set x before the for loop as well and change the iterator to
for i=1:size(x)
or something of that nature. Then you can index into x using x(i) and still have x as a vector and only look at a single value in x.
Protip: Use ctrl-i for proper indenting, as Stephen described in his comment. It really does make reading code easier for both you and us on MATLAB Answers.
Protip: Do not use MATLAB functions as variable names (e.g. length, count). Choose names like xlength or counter, even if you are not using the MATLAB functions. This way you will not override the MATLAB function now or in the future and will avoid any headaches that that may cause.
  1 个评论
Stephen23
Stephen23 2017-9-7
"Use ctrl-i for proper indenting... It really does make reading code easier for both you and us on MATLAB Answers."
Code that is easier to read is easier to understand. Code that is easier to understand has fewer bugs. Code that has fewer bugs wastes less of your life debugging.
Summary: Indent your code consistently!

请先登录,再进行评论。

更多回答(1 个)

Qudsiya  Irum
Qudsiya Irum 2017-9-8
编辑:Stephen23 2017-9-8
Thanks for comments! Please see the code, is this correct in indent??
NN=zeros(nx+1,nx+1);
xx=0.998;
for i=1:nx+1
counter=1;
for x=dx:xlength: nx+1
if(i-1)*xx<=x<=i*xx
i;
NN(i,counter)=(x-(i-1)*dx)/dx;
elseif i*xx<=x<=(i+1)*xx
i;
NN(i,counter)=((i+1)*dx-x)/dx;
end
NN(NN<0)=0;
counert=counter+1;
end
end
I want to obtain a NN matrix of (2501*2501) with condition that for interval (i-1)*xx<=x<=i*xx, it should return value 1 otherwise 0 for whole iteration. But I am not getting x as a vector at the end.please guide me.
  2 个评论
Stephen23
Stephen23 2017-9-8
"Please see the code, is this correct in indent??"
No, that is still a mess. You should use the default MATLAB indentation, which occurs automatically when you use the MATLAB editor.
Eric
Eric 2017-9-8
"If you want x to be the entire vector, set x=dx:length:nx+1 again on its own after the for loop."
As I mentioned previously, the way to get x as a vector with minimal changes to your code is to add the line
x =dx:xlength:nx+1
to the end of your code.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by