There is any function substituting for this 'for loops'?

1 Ansicht (letzte 30 Tage)
park minah
park minah am 19 Okt. 2011
for n = 1:99;
for a=1:size(A{n},1);
for m = n+1:100;
for b = 1:size(A{m},1);
if A{n}(a,:) == A{m}(b,:) ;
A{m}(b,:) = zeros(x,1);
% x = size(A{m},2)
end
end
end
end
end
A{n} has each different not only contents of cell but also size of cell. My meaning of this codes is willing to compare all columns of nth cell A with all columns of mth cell A.(n<m) If two columns are same,the column of mth cell will change into zeros.
for example,
A{1}=[1 0 1 1;1 1 1 1;0 0 1 0;]
A{2}=[1 1 0 0;1 1 0 1;0 0 1 0;]
A{3}=[1 1 1 1;0 0 1 1;1 0 1 1;]
The upper code makes them like this.
A{1}=[1 0 1 1;1 1 1 1;0 0 1 0;]
A{2}=[1 1 0 0;1 1 0 1;0 0 0 0;]
A{3}=[0 0 0 0;0 0 1 1;0 0 0 0;]
This code has no problem working, but it takes too long times. so, anybody knows a way to make this code more simply and quickly? frankly speaking, I don't want to use 'for' loops...hmm. I don't know it's possible..
I need your help.
PS. Each cell has a matrix, and its size is about 1000000X10. A size of A{n} continuously changes, but it is at least 50X1. In addition, a sameness of columns comes to 80%
  2 Kommentare
Naz
Naz am 19 Okt. 2011
Can you describe what you are trying to do? It's just difficult to comprehend it from the code.
Daniel Shub
Daniel Shub am 19 Okt. 2011
This is an optimization problem. It may be possible to eliminate some of the loops, but that may not improve performance. What we really need is an idea of how big A is, how big the matrices in A{n} are, and how often columns are the same. Without this it is hard to optimize.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 19 Okt. 2011
Just some simplification for your loop methods:
for n = 1:99
An = A{n}; % avoid repeated indexing
for a = 1:size(An, 1)
An_a = An(a, :);
for m = n+1:100
Am = A{m};
for b = 1:size(Am, 1)
if all(An_a == Am(b, :))
Am(b, :) = 0; % scalar expansion! zeros(x,1);
end
end
A{m} = Am;
end
end
end
Perhaps "if all(An_a == Am(b, :))" can be accelerated as:
if any(An_a - Am(b, :)) == 0

Weitere Antworten (2)

Daniel Shub
Daniel Shub am 19 Okt. 2011
I think with some reorganization you could transform the code so that it could be run in a parfor loop or on a cluster with the PCT. This could give you substantially speedup (limited only by the size of your cluster).

Naz
Naz am 20 Okt. 2011
A=zeros(3,4,3);
A(:,:,1)=[1 0 1 1;1 1 1 1;0 0 1 0];
A(:,:,2)=[1 1 0 0;1 1 0 1;0 0 1 0];
A(:,:,3)=[1 1 1 1;0 0 1 1;1 0 1 1];
Now you have a 3D matrix (or simply a stack of 2D matrices). Here you can compare columns buy subtracting them (if they are equal, the difference will be zero). Let's say you want to compare the columns of the top layer_1 (2D cell) with corresponding columns of the bottom layer_3 (2D cell):
n=1; % first cell
m=3; % third cell
z=size(A);r=z(1);c=z(2); % determine how many columns and rows
for k=1:c
if sum(abs(A(:,k,n)-A(:,k,m)))==0
A(:,k,n)=zeros(r,1); % set the c-th column of n-th matrix to zero
end
end
I hope I am not missing the main concept of the question

Kategorien

Mehr zu Mathematics finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by