MATLAB Answers

Fix the function hlp

6 views (last 30 days)
Ainars Cernavskis
Ainars Cernavskis on 26 Jan 2021
Edited: Walter Roberson on 28 Jan 2021
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
  2 Comments
Ainars Cernavskis
Ainars Cernavskis on 26 Jan 2021
thanks , i see it now .

Sign in to comment.

Accepted Answer

Jan
Jan on 26 Jan 2021
Edited: Jan on 26 Jan 2021
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?

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by