How solve this problem in Matlab

์กฐํšŒ ์ˆ˜: 4 (์ตœ๊ทผ 30์ผ)
Yeachan Choi
Yeachan Choi 2022๋…„ 3์›” 24์ผ
๋‹ต๋ณ€: David Goodmanson 2022๋…„ 3์›” 25์ผ
How can I write a program in a script file that creates and ๐‘› ร— ๐‘š matrix with elements that have the following values. The value of each element in the first row is the number of the column. The value of each element in the first column is the number of the row, The rest of the elements each has a value equal to the sum of the element above it and the element to the left.
  ๋Œ“๊ธ€ ์ˆ˜: 1
Yeachan Choi
Yeachan Choi 2022๋…„ 3์›” 24์ผ
ํŽธ์ง‘: Yeachan Choi 2022๋…„ 3์›” 24์ผ
First, I solved it with this code
n = input('Choose your number n : ')
m = input('Choose your number m : '); % n, m should be a positive integer
V = zeros(n, m);
V(1,:) = 1:m; V(:,1) = 1:n;
for a = 2:n;
for b = 2:m;
V(a,b) = V(a-1,b) + V(a,b-1)
end
end
but This code made me nervous, because there are many result of matrix in command file.

๋Œ“๊ธ€์„ ๋‹ฌ๋ ค๋ฉด ๋กœ๊ทธ์ธํ•˜์‹ญ์‹œ์˜ค.

์ฑ„ํƒ๋œ ๋‹ต๋ณ€

Davide Masiello
Davide Masiello 2022๋…„ 3์›” 24์ผ
ํŽธ์ง‘: Davide Masiello 2022๋…„ 3์›” 24์ผ
Note that your code displays a lot of matrixes because you haven't placed a semicolon when defining V. You can fix it easily
clear,clc
n = 5;
m = 6;
V = zeros(n,m);
V(1,:) = 1:m; V(:,1) = 1:n;
for a = 2:n;
for b = 2:m;
V(a,b) = V(a-1,b) + V(a,b-1);
end
end
V
V = 5ร—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
For the rest, I believe your code works correctly.

์ถ”๊ฐ€ ๋‹ต๋ณ€ (1๊ฐœ)

David Goodmanson
David Goodmanson 2022๋…„ 3์›” 25์ผ
Hi Davide,
here is a method that uses one for loop instead of two:
n = 5
m = 6
V = zeros(n,m);
V(:,1) = 1:n;
for k = 2:m
V(:,k) = cumsum(V(:,k-1)) +1;
end
V
V =
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

์นดํ…Œ๊ณ ๋ฆฌ

Help Center ๋ฐ File Exchange์—์„œ Linear Algebra์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ธฐ

ํƒœ๊ทธ

Community Treasure Hunt

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

Start Hunting!

Translated by