Creating a matrix such as a = [1 1 1 1 1 1 1; ... 1 2 2 2 2 2 2; ... 1 2 3 3 3 3 3; ... 1 2 3 4 4 4 4; ... 1 2 3 4 5 5 5; ... 1 2 3 4 5 6 6; ...

13 views (last 30 days)
Hello I am currently working on a project in which we must use the flexibility matrix of a mass-spring system in order to calculate its natural frequency. I've derived the flexibility matrix by hand and noticed the pattern below. How can I do this for N numbers instead of having to update by hand each time I increase N?
a = [1 1 1 1 1 1 1; ...
1 2 2 2 2 2 2; ...
1 2 3 3 3 3 3; ...
1 2 3 4 4 4 4; ...
1 2 3 4 5 5 5; ...
1 2 3 4 5 6 6; ...
1 2 3 4 5 6 7]

Accepted Answer

Image Analyst
Image Analyst on 11 Dec 2022
Here is one way - a simple for loop
N = 7;
% First way
a = ones(7);
for col = 1 : N
a(2:col, col) = 2 : col;
a(col : end, col) = col;
end
% Show in command window:
a
a = 7×7
1 1 1 1 1 1 1 1 2 2 2 2 2 2 1 2 3 3 3 3 3 1 2 3 4 4 4 4 1 2 3 4 5 5 5 1 2 3 4 5 6 6 1 2 3 4 5 6 7
% Second way
a = repmat((1:N)', 1, N);
for col = 1 : N
a(col:end, col) = col;
end
a
a = 7×7
1 1 1 1 1 1 1 1 2 2 2 2 2 2 1 2 3 3 3 3 3 1 2 3 4 4 4 4 1 2 3 4 5 5 5 1 2 3 4 5 6 6 1 2 3 4 5 6 7

More Answers (3)

John D'Errico
John D'Errico on 11 Dec 2022
Edited: John D'Errico on 11 Dec 2022
Hint #1. Can you just create the matrix from scratch each time? So, is there a simple formula for a(i,j) in the order n matrix? Answer: yes. All you need to see is that a(i,j) = min(i,j). If that is the case, then can you create the matrix using meshgrid? In fact, you can create it in one line of code, without needing meshgrid.
a_n = @(n) min(ones(n,1)*(1:n),(1:n)'*ones(1,n))
a_n = function_handle with value:
@(n)min(ones(n,1)*(1:n),(1:n)'*ones(1,n))
a_n(5)
ans = 5×5
1 1 1 1 1 1 2 2 2 2 1 2 3 3 3 1 2 3 4 4 1 2 3 4 5
So this function creats the entire matrix, given only the number n.
As an alternative hint, could you go from the matrix of order N, to N+1? How might you do that? For example, suppose you padded the previous order matriix with zeros on the left and top, and then added 1 to every element. Could you do that simply? (Of course.)
a_update = @(a) [zeros(1,size(a,2)+1);[zeros(size(a,1),1),a]] + 1;
a = 1
a = 1
a = a_update(a)
a = 2×2
1 1 1 2
a = a_update(a)
a = 3×3
1 1 1 1 2 2 1 2 3
a = a_update(a)
a = 4×4
1 1 1 1 1 2 2 2 1 2 3 3 1 2 3 4
So a_update creates the order n+1 matrix from the order n matrix.

Stephen23
Stephen23 on 11 Dec 2022
Edited: Stephen23 on 11 Dec 2022
N = 7;
A = min(1:N,(1:N).')
A = 7×7
1 1 1 1 1 1 1 1 2 2 2 2 2 2 1 2 3 3 3 3 3 1 2 3 4 4 4 4 1 2 3 4 5 5 5 1 2 3 4 5 6 6 1 2 3 4 5 6 7

Steven Lord
Steven Lord on 11 Dec 2022
A = gallery('minij', 7)
A = 7×7
1 1 1 1 1 1 1 1 2 2 2 2 2 2 1 2 3 3 3 3 3 1 2 3 4 4 4 4 1 2 3 4 5 5 5 1 2 3 4 5 6 6 1 2 3 4 5 6 7
See the documentation for the gallery function for a list of the other types of matrices it can create.

Categories

Find more on Creating and Concatenating 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!