フィルターのクリア

how to create this matrix [1 2 3 4 5;2 2 3 4 5;3 3 3 4 5;4 4 4 4 5;5 5 5 5 5]

14 ビュー (過去 30 日間)
Farooq Aamir
Farooq Aamir 2018 年 2 月 9 日
回答済み: Benjamin Drewry 2018 年 2 月 9 日
Waiting for your response
  1 件のコメント
John D'Errico
John D'Errico 2018 年 2 月 9 日
Why is it starting to look as if you are asking homework questions? This is now your second question on how to create a basic matrix?

サインインしてコメントする。

採用された回答

Jos (10584)
Jos (10584) 2018 年 2 月 9 日
編集済み: Jos (10584) 2018 年 2 月 9 日
% data
n = 7 % "order"
% engine
x = repmat(1:n,n,1)
x = max(x, x.')
Added after comment: for non-square matrices:
n = 4 ; m = 6 ; % n-by-m matrix
x = max(repmat(1:m,n,1), repmat(1:n,m,1).')
  2 件のコメント
Farooq Aamir
Farooq Aamir 2018 年 2 月 9 日
How it can work for rectangular matrices @ jos
Jos (10584)
Jos (10584) 2018 年 2 月 9 日
See updated answer

サインインしてコメントする。

その他の回答 (3 件)

Andrei Bobrov
Andrei Bobrov 2018 年 2 月 9 日
[x,y] = ndgrid(1:5)
out = max(x , y)
  3 件のコメント
Stephen23
Stephen23 2018 年 2 月 9 日
+1 nice and simple
Andrei Bobrov
Andrei Bobrov 2018 年 2 月 9 日
@Stephen: Thank you, Stephen!
@Farooq Aamir:
m = 5; n = 8;
[x,y] = ndgrid(1:m,1:n);
out = max(x , y);

サインインしてコメントする。


Torsten
Torsten 2018 年 2 月 9 日
編集済み: Torsten 2018 年 2 月 9 日
A = [1 2 3 4 5;2 2 3 4 5;3 3 3 4 5;4 4 4 4 5;5 5 5 5 5];
Best wishes
Torsten.
  1 件のコメント
Farooq Aamir
Farooq Aamir 2018 年 2 月 9 日
Thanks for your reply but how to generalize this matrix for higher order

サインインしてコメントする。


Benjamin Drewry
Benjamin Drewry 2018 年 2 月 9 日
Well super brute force method, but it helps if you are just beginning to code
clear all; close all; clc
M=5; N=5; % can vary M and N to matrix of size of interest
% preallocate matrix space
concat_matrix=ones(M,N);
j=1;
for i=1:M
if i>N
concat_matrix(i, :)=concat_matrix(i-1,:)+1;
else
repval=ones(1,j).*j;
remains=[numel(repval)+1:N];
concat_matrix(i,1:N)=[repval remains];
j=j+1;
end
end

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by