How to make a loop to add 3 elemts in matrix of zeros(n,n)?

1 次查看(过去 30 天)
n= input('enter the value of n:-->');
A= zeroes(n,n); %n must be greater than 3
i want to add [1 4 1] in second row after skipping first 0 if n=4
and if n=5 add [1 4 1] in second row after skipping first 0 and add [ 1 4 1] to third row after skip two 0`s.
for example:
A = [0 0 0;
0 0 0;
0 0 0]
if n=3
add [ 1 4 1] to 2nd row
then
A = [ 0 0 0;
1 4 1;
0 0 0]
if n=4
A = [0 0 0 0;
0 1 4 1;
0 0 0 0]
if n=5
A = [ 0 0 0 0 0;
1 4 1 0 0;
0 1 4 1 0;
0 0 1 4 1;
0 0 0 0 0];
similarly ahead
help me

采纳的回答

Mikhail
Mikhail 2014-8-22
编辑:Mikhail 2014-8-22
As i understand, u need this:
n= input('enter the value of n:-->');
A= zeroes(n,n); %n must be greater than 3
for i=2:n-1 % loop from 2nd to (n-1) row
A(i,:)=[zeros(1,i-2),[1 4 1],zeros(1,n-i-1)] %explicit structure of row i
end
  10 个评论
Avtar Singh
Avtar Singh 2014-8-23
clear all
a=[1,1,0;4,4,1;7,5,1;15,2,1;6,4,3]
n = input('enter the number of passing points of curve:--->>')
A= zeros(n,n); %n must be greater than 3
for i=2:n-1 % loop from 2nd to (n-1) row
A(i,:) = [zeros(1,i-2),[1 4 1],zeros(1,n-i-1)]; %explicit structure of
row i
end
A(1,1)=1;
A(n,n)=1;
R=[0 0 0; 3*(a(3,:)-a(1,:)); 3*(a(4,:)-a(2,:));3*(a(5,:)-a(3,:));0 0 0];
X=inv(A)
T=X*R;
u=[0:0.01:1]'
U=[u.^3 u.^u u u.^0];
M=[2 -2 1 1;-3 3 -2 -1 ;0 0 1 0;1 0 0 0];
B=[0 0 0;a(1,:);0 0 0;T(2,:)];
P=U*M*B;
plot3(P(:,1),P(:,2),P(:,3));
hold on
B1=[a(1,:);a(2,:);T(2,:);T(3,:)];
P1=U*M*B1;
plot3(P1(:,1),P1(:,2),P1(:,3));
I want to join these curves by smooth one but i cant help me

请先登录,再进行评论。

更多回答(0 个)

类别

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