Fix the function hlp

6 次查看(过去 30 天)
Ainars Cernavskis
Ainars Cernavskis 2021-1-26
评论: Rena Berman 2023-11-27
i was told to write funciton for this: but it keeps saying something is wrong but i dont know what is wrong with it .
function [ arr ] =special_matrix( n )
if n>2
for i = 1:n
for j = 1:n
if mod(j,3)==0
arr(i,j)}=((i-1)*n+j}^j;
else
arr(i,j)={i-1}*n+j;
end
end
end
else
disp('Error; n must be greater than i')
end
end
  3 个评论
Sam Chak
Sam Chak 2023-11-23
The original question may be beneficial for people who wish to learn how to create special square matrices.
Rena Berman
Rena Berman 2023-11-27
(Answers Dev) Restored edit

请先登录,再进行评论。

采纳的回答

Jan
Jan 2021-1-26
编辑:Jan 2021-1-26
By the way, this can be done in one line:
arr = reshape(1:n^2, n, n).' .^ ((1:n) .^ rem(0:n-1, 2))
Have fun with analyzing, how this works.
Is there a shorter solution?

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by