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 件のコメント

Walter Roberson
Walter Roberson 2021 年 1 月 26 日
arr(i,j)}=((i-1)*n+j}^j;
You have two } each not matched by previous {
{} in MATLAB is only used for cell arrays or working with composite objects like tables. It is never used for prioritization. prioritization is only through () brackets
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 日

0 投票

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 件)

カテゴリ

タグ

質問済み:

2021 年 1 月 26 日

コメント済み:

2023 年 11 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by