Creating two arrays using Arrayfun

How can I create two array sets with Arrayfun, I am getting an error with this code structure. thanks
%%Simulink Matlab function Block
function [y,x]=fcn(Az,sm,del,w0,gama,wc,D_ws,e,n_xs,n_ys,n_zs,om,s)
a= 1:1:100;
vc=1:1:100;
y=arrayfun(@fcn,mod(a,10)+1,fix((a-1)./10)+1);
x=arrayfun(@fcn,fix((vc-1)./10)+1);
function [b,c]=fcn(ns,nb,nc)
k_x=0.01;
k_y=0.01;
b=-w0*cos(gama(nb))+ (wc-k_x*w0)*cos(gama(nb)) * (e*cosd(Az(nb))+sm(ns)*cosd(Az(nb)+del(nb))) + ((D_ws+k_y*w0)*cos(gama(nb))) * (e*sind(Az(nb))+sm(ns)*sind(Az(nb)+del(nb)));
c=-n_xs*sin(gama(nc))*cosd(Az(nc)+del(nc)) + n_ys*sin(gama(nc)) * sind(Az(nc)+del(nc)) + n_zs*cos(gama(nc)) + (e/om) * (cos(gama(nc)) * (s(2) * cosd(Az(nc)) + s(1)*sind(Az(nc))) - sin(gama(nc))*sin(del(nc))*(s(3)-om));
end
end

6 Commenti

dpb
dpb il 28 Gen 2023
Code formatted by dpb...
What are you trying to do here????
You've got nested functions recursive and duplicated function names and even ignoring that, none of the references to them match a function prototype of the same name.
Explain the end object here...
Alexi
Alexi il 28 Gen 2023
Modificato: dpb il 28 Gen 2023
With the code I wrote above, I tried to create a 100-valued y-array and a 10-valued x-array. I'm trying to create a more efficient code structure compared to a for loop. I am developing a model on simulink. I use matlab fucntion blocks while developing the model. Repetitive processes such as creating arrays, transferring arrays between blocks are often dominated in the content of Math function blocks. I use a for loop when creating arrays and storing data in matrix content, but I noticed that the for loop blocks simulink blocks and slows down the simulation a lot.
more understandable code structure that does the same function as above.
Example code: what ı want to do ------ obtained f array (1,100)
m=10;
n=10;
k=1;
f=zeros(1,m*n);
for i=1:1:m*n
f(i)=x(k)+y(i)...;
if k==n
k=0;
end
k=1+k;
end
"but I noticed that the for loop blocks simulink blocks and slows down the simulation a lot. "
A well-written loop will be faster than ARRAYFUN.
dpb
dpb il 28 Gen 2023
Modificato: dpb il 28 Gen 2023
for loops will be far more efficient than arrayfun; while it's one line of source code, it has to parse the inputs and then build the for...end loop internally to execute. Don't confuse lines of code with efficiency; they're not necessarily at all in conformance with fewer source lines-->faster execution time.
USE THE CODE FORMATTING BUTTON FOR POSTING CODE!!!!
The above uses undefined variables x,y; what are they?
More than likely the arrays can be constructed more efficiently with colon operator and matrix operations unless they're recursive although even then it's probably possible to make use of patterns if have the definition of what it is that is the end result desired.
dpb
dpb il 28 Gen 2023
Modificato: dpb il 28 Gen 2023
Assuming x, y are commensurately-sized arrays, the above would appear to be equivalent to
f=repmat(x(:),m,1)+y(:);
which would be "the MATLAB way"...
Thanks for your replies, I'll review it again.

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Simulink in Centro assistenza e File Exchange

Prodotti

Release

R2019a

Richiesto:

il 28 Gen 2023

Modificato:

dpb
il 28 Gen 2023

Community Treasure Hunt

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

Start Hunting!

Translated by