How do I make an array, (m,n), where the first row corresponds to the row number, where the first column corresponds to the column number, and the remaining elemnts by adding

7 views (last 30 days)
function C = Special_Array(m ,n)
% This function takes an arbitrary m and n positive integers representing
% the dimensions of the output array C.
C = ([1:m;1:n]);
This is the code I have so far which create part 1 and 2 of the array however the 3rd part im unsure how to do.
  1. the elements in the first row correspond to the row number
  2. the elements in the first column correspond to the column number
  3. the rest of the elements are calculated as the sum of the element above it and the element to the left
I beleive you're supposed to use the for command

Accepted Answer

KSSV
KSSV on 26 Mar 2022
m = 5; n = 4 ;
A = zeros(m+1,n+1) ;
A(1,2:n+1) = 1:n ;
A(2:m+1,1) = 1:m ;
for i = 1:m
for j = 1:n
A(i+1,j+1) = i+j ;
end
end
A
A = 6×5
0 1 2 3 4 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9
  3 Comments
KSSV
KSSV on 26 Mar 2022
m = 3 ;
n = 4 ;
C = [1 2 3 4
2 4 7 11
3 7 14 25] ;
A = zeros(m,n) ;
A(1,:) = 1:n ;
A(:,1) = 1:m ;
for i = 2:m
for j = 2:n
A(i,j) = A(i-1,j)+A(i,j-1) ;
end
end
A
A = 3×4
1 2 3 4 2 4 7 11 3 7 14 25

Sign in to comment.

More Answers (1)

Matt J
Matt J on 26 Mar 2022
Edited: Matt J on 26 Mar 2022
n=6;
tmp=pascal(n+1);
output=tmp(2:end,2:end)-pascal(n),
output = 6×6
1 2 3 4 5 6 2 4 7 11 16 22 3 7 14 25 41 63 4 11 25 50 91 154 5 16 41 91 182 336 6 22 63 154 336 672

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!