how to get an matrix as an output in a function

I have the following function I do not know why it is not working. Would you please help me?
function [L_e]=L_element(e,nnpe,nn)
% Gather matrix
L_e=zeros(nnpe,nn);
for i=1,nnpe
for j=1,nn
if j==(nnpe-1)*(e-1)+i
L_e(i,j)=1;
end
end
end
end

 Accepted Answer

Rik
Rik on 23 Feb 2020
Edited: Rik on 23 Feb 2020
The mlint is giving you a hint: it wants you to put a semicolon to suppres output. Why would you have an output with the for loop? Because you put a comma instead of a colon:
function [L_e]=L_element(e,nnpe,nn)
% Gather matrix
L_e=zeros(nnpe,nn);
for i=1:nnpe
for j=1:nn
if j==(nnpe-1)*(e-1)+i
L_e(i,j)=1;
end
end
end
end
Of course you can also avoid the nested loop:
function [L_e]=L_element(e,nnpe,nn)
[I,J]=ndgrid(1:nnpe,1:nn);
L_e= J==(nnpe-1)*(e-1)+I ;
L_e=double(L_e);
end

5 Comments

Thanks for your answer. I would like to get the L_e equation for each element or e in that function. Would you please help me to get the code working. Thanks.
Both functions in my answer return a matrix (the same in fact). What is your question?
" Would you please help me to get the code working"
The code in Riks answer is working: Rik fixed your code and explained exactly what the problem was.
Thanks for your response it is working now.
Don't accept the answer

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

on 23 Feb 2020

Commented:

on 24 Feb 2020

Community Treasure Hunt

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

Start Hunting!