how to get an matrix as an output in a function

조회 수: 4 (최근 30일)
Faezeh Manesh
Faezeh Manesh 2020년 2월 23일
댓글: darova 2020년 2월 24일
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

채택된 답변

Rik
Rik 2020년 2월 23일
편집: Rik 2020년 2월 23일
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
Faezeh Manesh
Faezeh Manesh 2020년 2월 23일
Thanks for your response it is working now.
darova
darova 2020년 2월 24일
Don't accept the answer

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by