How to create this matrix not using loops and if/else statements?
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!