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

1 view (last 30 days)
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

Accepted Answer

Mikhail
Mikhail on 22 Aug 2014
Edited: Mikhail on 22 Aug 2014
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 Comments
Avtar Singh
Avtar Singh on 23 Aug 2014
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

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!