フィルターのクリア

Calculate values only once in the loop

6 ビュー (過去 30 日間)
D_coder
D_coder 2018 年 9 月 12 日
コメント済み: D_coder 2018 年 9 月 12 日
Suppose I have a code running in a loop I have done some operations and I create a matrix in the loop, that matrix has the same size in every iteration. How do i calculate the number of elemens of the matrix only for the first iteration and not for every iteration since i know that it will be the same for every iteration? I want to avoid using ifelse statements.

回答 (2 件)

KSSV
KSSV 2018 年 9 月 12 日
Read about function numel. This will give you the number of elements present. Also you can get size of matrix and do product to get the elements.
  5 件のコメント
KSSV
KSSV 2018 年 9 月 12 日
N = 0 ; % number of elements
for i = 1:10
A = rand(10) ;
while ~N
N = numel(A) ;
end
end
D_coder
D_coder 2018 年 9 月 12 日
is there anyother way to calculate without using loops or if else statement. How about N(N==0) = execute the statement

サインインしてコメントする。


Amir Xz
Amir Xz 2018 年 9 月 12 日
Use "isempty":
I = rand(4,5);r=[]; c=[];
for i=1:10
if isempty(r) || isempty(c)
[r,c]=size(I);
end
end

カテゴリ

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