How can I create a matrix (4x10 for example) like this and insert it into a loop?
Show older comments
Hello dear users of MATLAB,
My question is that how can I create a matrix (4x10 for example) like this and insert it into a loop:
(-5 -4 -3 -2 -1 0 1 2 3 4 5)
( 0 -4 -3 -2 -1 0 1 2 3 4 0)
( 0 0 -3 -2 -1 0 1 2 3 0 0)
( 0 0 0 -2 -1 0 1 2 0 0 0)
My code is this:
%entering data
K = 51.5; %W/m*K
T0 = 300; %K
q = 300; %W
v = 1; %m/s
rho = 7700; %kg/m3
C = 444; %j/kg*C
n=10;
m=10;
lambda = (((C*rho/K)))/2;
%creating vectors
X=zeros(m,n);
Z=zeros(m,n);
% Y=zeros(m,n);
Tmax=zeros(m,n);
Y=0;
% for i = 1:n
% Y(i)=0;
% end
for i = 1:n
for j = 1:m
X(i,j)=-0;005+(i-1)*10/(n-1);
Z(i,j)=0+(j-1)*(-0.00005)/(m-1);
K1(i,j)=exp(-lambda*X(i,j).*v);
R(i,j) = sqrt(X(i,j).^2+Y.^2+Z(i,j).^2);
K2(i,j)=1./R(i,j)*exp(-lambda*R(i,j)*v);
d(i,j)=K1(i,j)*K2(i,j)*1.;
Tmax(i,j)=T0+d(i,j)*(q*0.6/(2*pi*K));
end
end
% subplot(3,1,1), contour(X,Z,Tmax), colormap
% title('Temperature (Steady State)'),ylabel('Z'),xlabel('X'),colorbar
% camroll(-180)
% subplot(3,1,2), pcolor(X,Z,Tmax), shading interp
% title('Temperature (Steady State)'),ylabel('Z'),xlabel('x'),colorbar
% camroll(-180)
The problem is with X and Z.
Answers (1)
KSSV
on 19 Oct 2020
row = -5:+5 ;
iwant = zeros(4,length(row)) ;
iwant(1,:) = row ;
for i = 2:4
iwant(i,i:end-i+1) = row(i:end-i+1) ;
end
iwant
Categories
Find more on MATLAB 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!