フィルターのクリア

how to get an matrix as an output in a function

3 ビュー (過去 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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by