Calculate values only once in the loop
Mostrar comentarios más antiguos
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.
Respuestas (2)
KSSV
el 12 de Sept. de 2018
0 votos
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 comentarios
KSSV
el 12 de Sept. de 2018
for i = 1:N
if i == 1
N = numel(A) ;
end
end
KALYAN ACHARJYA
el 12 de Sept. de 2018
Editada: KALYAN ACHARJYA
el 12 de Sept. de 2018
@KSSV Sir, I also looking for the answer, I tried but didn't get the way
%If the condition is true, then only execute the statement
%How to avoid if statement in above code?
N(condition true)=execate the statement
KSSV
el 12 de Sept. de 2018
N = 0 ; % number of elements
for i = 1:10
A = rand(10) ;
while ~N
N = numel(A) ;
end
end
D_coder
el 12 de Sept. de 2018
Amir Xz
el 12 de Sept. de 2018
Use "isempty":
I = rand(4,5);r=[]; c=[];
for i=1:10
if isempty(r) || isempty(c)
[r,c]=size(I);
end
end
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!