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)
Show older comments
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.
- the elements in the first row correspond to the row number
- the elements in the first column correspond to the column number
- 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
0 Comments
Accepted Answer
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
3 Comments
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
See Also
Categories
Find more on Resizing and Reshaping Matrices 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!