How to Create function for developing n*n matrix?

I need to create a function .The input argument of the function is an odd integer number n. (e.g. 1, 3, 5 …). Depend on n, create a n by n matrix. In the center of the matrix you start with 1. and . Clockwise you place the next numbers. The output of your function is the sum of the only odd numbers in the matrix.

3 个评论

This sounds like a homework assignment. If it is, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the MATLAB Onramp tutorial (https://www.mathworks.com/support/learn-with-matlab-tutorials.html) to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.
function [oddmat,summodd] = fnoddsum (n,oddmat)
oddmat=randi((n.*n),n);
summodd=sum(oddmat(rem(oddmat,2)==1));
end
I'm new to matlab and I'm following tutorials provide in the website. But for the posted question I developed above fuction and I'm not sure how to define a matrix with odd number in clockwise direction starting from 1 exactly in the centre. So if you could help me how to define array with specific condition?

请先登录,再进行评论。

 采纳的回答

There's already a built in demo for this. Type help spiral or open it
n = 5;
A = spiral(n)
s = sum(1:2:n^2)

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by