How to create this matrix not using loops and if/else statements?

1 回表示 (過去 30 日間)
sznailc
sznailc 2021 年 10 月 17 日
編集済み: Can Atalay 2021 年 10 月 17 日
Hello. I have a problem with creating matrix like this without using loops and if/else statements. It must have 5 columns and N rows.
0 1 1 1 0/(N-1)
0 1 1 2 1/(N-1)
. . . 3 2/(N-1)
. . . . .
0 1 1 N (N-1)/(N-1)
I ended up with this:
A1 = [0 1 1 N (N-1)/(N-1)];
A2 = reshape(A1, [], N);
A = repmat(A2, N, 1)
----------------------------
(N = 5)
ans =
0 1 1 5 1
0 1 1 5 1
0 1 1 5 1
0 1 1 5 1
0 1 1 5 1

採用された回答

Can Atalay
Can Atalay 2021 年 10 月 17 日
編集済み: Can Atalay 2021 年 10 月 17 日
number_of_columns = 5;
number_of_rows = 10; % you can change this to N
first_column = zeros(number_of_rows,1);
second_and_third_columns = ones(number_of_rows,2);
fourth_column = transpose(1:number_of_rows);
fifth_column = transpose(0:(number_of_rows-1))/(number_of_rows-1);
end_result = [first_column second_and_third_columns fourth_column fifth_column]
% end_result = [first_column, second_and_third_columns, fourth_column, fifth_column]
% the line above would also work
Did I get the question right? This should work if you change the second line's value from 10 to N

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by