Borrar filtros
Borrar filtros

I want to save the data of the rows of U that are not empty. However, since this is inside a loop, I keep deleting the data from the previous loop. How can I keep saving the data without deleting the previous one?

1 visualización (últimos 30 días)
if isempty(U)==0
u = U(any(U,2),:);
U=[];
end
On the first iteration, it works how it should, but on the second iteration it will overwrite the previous data saved in "u". How can I fix this? It should keep saving on the next row, not deleting the previous data.
  2 comentarios
Adam
Adam el 9 de En. de 2017
Editada: Adam el 9 de En. de 2017
Please give the full relevant code, including the loop. Just the code inside the loop is not very helpful.
It isn't obvious what U is because a numeric array cannot have empty elements in it other than being empty itself.
Eduardo Rocha
Eduardo Rocha el 9 de En. de 2017
Editada: dpb el 9 de En. de 2017
while n<size(Preos2013S1,1)/20 || N<100
X=zeros(50,7);
for k=1:size(X,2)
for j=1:size(X,1)
X(j,k)=perce_min(1,k)+rand(1,1)*(perce_max(1,k)-perce_min(1,k));
end
end
D=pdist2(X,classesalgimunS1);
T=zeros(size(D,1),1);
for k=1:size(D,2)
A(k)=min(D(:,k));
end
for k=1:size(D,1)
for j=1:size(D,2)
if D(k,j)<0.4 && D(k,j)==A(j) && b(j)==0
T(k)=T(k)+1;
end
end
if T(k)>(size(classesalgimunS1,1)/size(X,1))*3
U(k,:)=X(k,:);
end
end
if isempty(U)==0
u = U(any(U,2),:);
U=[];
end
nclasse=max(b);
for k=1:size(u,1)
for j=1:size(A,2)
if pdist2(u(k,:), classesalgimunS1(j,:))==A(j) && pdist2(u(k,:), classesalgimunS1(j,:))<0.4 && b(j)==0
b(j)=nclasse+k;
n=n+1;
end
end
end
N=N+1;
end

Iniciar sesión para comentar.

Respuesta aceptada

dpb
dpb el 9 de En. de 2017
In most rudimentary to make work, add
u=[]; % initialize
before beginning the outer loop and then
if ~isempty(U)
u = [u; U(any(U,2),:)]; % accrete new U into u
U=[];
end
This has the issue of dynamic reallocation of u on each pass, but if isn't terribly long the runtime penalty shouldn't be too great. If it does bog down, then preallocate a very large array and populate it explicitly by keeping track of number of elements added each pass by
nU=size(U,1);
and a running total for the next insertion location into the array. And, of course, have to check don't overrun it and reallocate more room, etc., etc., if do. But, for all but the extreme cases the first solution should be just fine; wouldn't worry about the other details until after have shown that is a real performance issue on real data.
  2 comentarios
dpb
dpb el 9 de En. de 2017
Editada: dpb el 9 de En. de 2017
Do you foresee doing this a significant number of further times? If so, the effort to preallocate for u isn't that great, the general idea is
nMaxU=SomeLargeNumberGreaterThanExpectedLength;
u=zero(nMaxU,nColOfu);
idu=1;
...
if ~isempty(U)
U=U(any(U,2),:);
idu2=idu1+size(U,1)-1;
if(idu2>nMaxU)
% additional reallocation of more room here
...
else
u(idu1:idu2,:)=U;
idu1=idu2+1;
U=[];
end
Should take very little to flesh out the remainder...

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by