Passing array as input to a function

조회 수: 5 (최근 30일)
Hung Yu
Hung Yu 2023년 8월 23일
댓글: Dyuman Joshi 2023년 8월 23일
Hi, im trying to pass the following arrays into the self created function DrawRegularPolygon but Matlab is prompting me matrix dimension errors. Can someone help
n = [3,4] ;
D = [2,1] ;
a = [0,0] ;
[xseries, yseries] = DrawRegularPolygon ( n , D , a) ;
function [xseries, yseries] = DrawRegularPolygon ( nosides , Diameter , alpha )
ThetaSeries = ( (0 + alpha).*pi ) : ( 2*pi./nosides ) : ( (2 + alpha).*pi ) ;
xseries = (Diameter./2) .* cos(ThetaSeries) ;
yseries = (Diameter./2) .* sin(ThetaSeries) ;
end
  댓글 수: 2
Stephen23
Stephen23 2023년 8월 23일
The outputs are different sizes. You could easily use ARRAYFUN:
n = [3,4] ;
D = [2,1] ;
a = [0,0] ;
[xseries, yseries] = arrayfun(@DrawRegularPolygon, n , D , a, 'uni',false)
xseries = 1×2 cell array
{[1 -0.5000 -0.5000 1]} {[0.5000 3.0616e-17 -0.5000 -9.1849e-17 0.5000]}
yseries = 1×2 cell array
{[0 0.8660 -0.8660 -2.4493e-16]} {[0 0.5000 6.1232e-17 -0.5000 -1.2246e-16]}
function [xseries, yseries] = DrawRegularPolygon ( nosides , Diameter , alpha )
ThetaSeries = ( (0 + alpha).*pi ) : ( 2*pi./nosides ) : ( (2 + alpha).*pi );
xseries = (Diameter./2) .* cos(ThetaSeries);
yseries = (Diameter./2) .* sin(ThetaSeries);
end
Dyuman Joshi
Dyuman Joshi 2023년 8월 23일
I did consider using arrayfun, but it seems my bias against using it has grown quite a bit.

댓글을 달려면 로그인하십시오.

답변 (1개)

Dyuman Joshi
Dyuman Joshi 2023년 8월 23일
편집: Dyuman Joshi 2023년 8월 23일
If you perform colon operations with vectors to create regularly-spaced vectors, it will only take the 1st element of each vector in consideration.
From the documentation of colon, : - "If you specify nonscalar arrays, then MATLAB interprets j:i:k as j(1):i(1):k(1)."
%Example
[1 2]:[0.5 0.75]:[3 4]
ans = 1×5
1.0000 1.5000 2.0000 2.5000 3.0000
You can use a for loop to make regularly-spaced vectors corresponding to each element of inputs, and since number of elements of the generated vector will vary, use a cell array to store them -
n = [3,4] ;
D = [2,1] ;
a = [0,0] ;
C = DrawRegularPolygon ( n , D , a)
C = 2×2 cell array
{[ 1 -0.5000 -0.5000 1]} {[0.5000 3.0616e-17 -0.5000 -9.1849e-17 0.5000]} {[0 0.8660 -0.8660 -2.4493e-16]} {[ 0 0.5000 6.1232e-17 -0.5000 -1.2246e-16]}
function C = DrawRegularPolygon ( nosides , Diameter , alpha )
n = numel(nosides);
%2 row cell array - 1st row for xseries, 2nd row for yseries
C = cell(2,n);
%Assuming all the input variables have same number of elements
for k=1:numel(nosides)
ThetaSeries = ( (0 + alpha(k)).*pi ) : ( 2*pi./nosides(k) ) : ( (2 + alpha(k)).*pi );
xseries = (Diameter(k)./2) .* cos(ThetaSeries);
yseries = (Diameter(k)./2) .* sin(ThetaSeries);
C{1,k} = xseries;
C{2,k} = yseries;
end
end
P.S - You can use a check in the code as well to see if the inputs have similar dimensions or not.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by