Creating a vector from nested for loops

Hi
So I basically want to create a column vector from two nested for loops
Initially, i planned my code to be like
for i =1:n
for j=1:m
P=(j-1)*n+i;
b(P,1)=T((i-1)*dx,(j-1)*dy); %%T is an anonymous function T=@(x,y) 20*cos(x)*sin(y);
end
end
But when I run that I get a singular row vector (rank 1) and I can't use that for later on when I need to divide it by a matrix A.
What am I doing wrong over here?
Many thanks

답변 (1개)

Cris LaPierre
Cris LaPierre 2020년 3월 26일

0 개 추천

Two ideas come to mind.
for i =1:n
for j=1:m
b(i,j)=...
end
end
b=b(:);
Another way might be to add the new result to the end of b
b=[];
for i =1:n
for j=1:m
b=[b; T((i-1)*dx,(j-1)*dy)];
end
end

댓글 수: 7

Maaz Madha
Maaz Madha 2020년 3월 26일
I ran these two codes but they aren't working as I need mine to be 20301*1 and these aren't. Also, I need to implement the P function as that is needed.
Cris LaPierre
Cris LaPierre 2020년 3월 26일
I can't debug without more info from you. What is n? m? Does nxm=20301?
You can still create P. You just don't need it for creating your column vector b.
%%Initialisation 1
alpha=0.1;
beta=0.1;
dx=(2*pi)/100;
dy=dx;
dt=0.01; %%timeskip
nu=0.1;
H=2*pi;
L=4*pi;
t_values=[0,0.1,0.5,1,2,5];
T_end=5;
%Mesh
n=round((L/dx)+1);%%lecture notes 21/1/2020. Converts rectangle to a mesh
m=round(((H/dy)+1));
t=round((T_end/dt)+1);
x=[0:dx:L]';
y=[0:dy:H]';
v=@(x,t) 5*(1-(x-2*pi)^2/(4*pi^2)).*cos(pi*t)*sin(x);
T=@(x,y) 20*cos(x)*sin(y);
b=zeros(n*m,1);
A=zeros(n*m);
n is 201 and m is 101 so if you multiply them you get 20301. The P is a pointer function which helps map 2D matrix into a 1D one
Try this. You calculation of P was incorrect for this purpose
for i =1:n
for j=1:m
P=(i-1)*m + j;
b(P,1)=T((i-1)*dx,(j-1)*dy);
end
end
Maaz Madha
Maaz Madha 2020년 3월 26일
I tried your method of P and it now shows Warning: Matrix is singular to working precision. I would also like to ask how you got that value of P as I would like to know your method of reasoning
Cris LaPierre
Cris LaPierre 2020년 3월 26일
Let's walk through the code.
i=1
j=1
store the current value of T in b(1,1)
i=1
j=2
store the current value of T in b(2,1)
...
i=1
j=m
store the current value of T in b(m,1)
i=2
j=1
What row of b should the value of T be stored in? It should be m+1, right?
Plug the current values of i and j into my equation for P.
P=(2-1)*m + 1 which equals m+1
Test this for the first case (i=1,j=1).
P=(1-1)*m + 1 or 1.
The outer loop increments one, then the inner loop increments though all it's loops. You wrote P for the opposite case - the outer loop running through all it's values and then the inner loop increments one.
Cris LaPierre
Cris LaPierre 2020년 3월 26일
As for P, you're doing something somewhere else that is not shown that is causing your error. You can read more about that here, but that's a separate issue that is not related to the question asked in this post.

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2020년 3월 26일

댓글:

2020년 3월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by