FAST algorithm to jenga matrix?

matlab

    Next

  • 1. Displaying date on plot axis correctly
    I have a surf plot on which I display the date on x-axis. I have defined a vector for the labels and this displays as I want it but the axis still displays 'x10^5' against the scale on this axis. How do I get rid of this from the display? Please help as I cannot seehow to do this.
  • 2. can't load text file with header
    Hello Matlab Community, I have thousands of text files that appear like: 2/14/2009 9:16:14 PM -90.332031E-3 -41.503906E-3 -57.373047E-3 -63.476562E-3 -109.863281E-3 -106.201172E-3 -61.035156E-3 % and this carries on for 20,000 points I use the command "load(text.txt)" and i get "error in line one" or something like that, I tested one file without the time stamp header "5/14/2009 9:16:14 PM" and it loads normally but I have to have the header there because i need to know when the event happened (so i can make frequency plots), any ideas? if i can't get this to work my boss will fire me and my girl will leave me, (slight dramatization...) I would appreciate any help Cheers! Greg
  • 3. simple matrix exponentiation question
    Hi all, I'm trying to perform the following operation: I have a n x k matrix A and a n x 1 vector B. I want to raise all the numbers in the first row of A to the B(1)th power, and similarly, all numbers in the kth row of A to the B(k)th power. I don't want to use loops - the matrices are large, and vectorised code is important since speed is a concern. It's a pretty simple operation, but I cant find out how to do it - any suggestions?
  • 4. Output Format in GUI
    I've just started teaching myself how to make GUIs in MATLAB. I'm creating a MATLAB GUI and I have some edit text boxes reserved for the output. I would like for the format of the output to be long g. However, I can only seem to get it to be default. Is there a way to fix this? I know "format long g" can achieve this, I just don't know where to put it in my m-file. Thanks, Kyle
  • 5. Cell contents reference from a non-cell array object...but only in GUIDE...
    I'm running GUIDE and am generating a GUI to interact with a simulation. Basically, I'm trying to create a cell array to contain a log of simulation-generated messages. I have initialized the cell array as a global variable so that it can be updated in different functions. Since I don't want to lose previous messages (they're being saved to get written to file later), the array needs to dynamically concatenate itself with the messages that come in. I have a little code snippet that works in the command window, but when I implement it into the rest of my code, I get my favorite error message: Cell contents reference from a non-cell array object Here's the code: EventLog = {EventLog{1,:}, NewMsg}; Since EventLog ={} when it's initialized as global, it grows each time the previous command is run...theoretically. Oh, I might also mention that simultaneously, I am trying to use EventLog as the 'String' input for a listbox control: i.e. set(handles.listbox,'String',EventLog); Any ideal?

FAST algorithm to jenga matrix?

Postby Hoi Wong » Mon, 30 Mar 2009 11:17:01 GMT

I have a GIANT sparse matrix with scattered non-trivial elements ranging from 1:N, then I need to remove a M non-trivial elements and maintain the cardinality from 1:N-M. Is there a fast way to do it? Here's an example to illustrate what I'm trying to do (N=8, M=2):

A =
     0     0     8     0     0
     1     4     0     0     2
     0     5     3     0     0
     7     0     0     6     0
	 
ans =
     1
     2
     3
     4
     5
     6
     7
     8	 
	 
------------------------------
Numbers to delete:  2, 6

	 
desiredMatrix =
     0     0     6(-2) 0     0
     1     3(-1) 0     0     X
     0     4(-1) 2(-1) 0     0
     5(-2) 0     0     X     0
	 
desiredMatrix =
     0     0     6     0     0
     1     3     0     0     0
     0     4     2     0     0
     5     0     0     0     0
	 
ans =
     1
     2
     3
     4
     5
     6

Because the matrix is huge (but sparse), I don't want to use for loops. I kind of have a feeling that there must be a slick way to do it with a different way of thinking, and there must be some MATLAB master-minds here who can come up with a slick one-or-two-liners. :)

Thanks in advance

Re: FAST algorithm to jenga matrix?

Postby Roger Stafford » Mon, 30 Mar 2009 16:11:03 GMT



  I'm afraid this isn't a one- or two-liner, but an eight-liner, Hoi.  I couldn't think of anything shorter.  Let d be a vector of the numbers to "delete".

 [r,c,v] = find(A);
 [v,p] = sort(v);
 v = 1;
 v(d) = 0;
 v = cumsum(v);
 v(d) = 0;
 v(p) = v;
 B = sparse(r,c,v,size(A,1),size(A,2)); 

B is the desired matrix.

  This depends on your statement that the non-zero elements of A are 1:N and those of B are are to be 1:N-M where M = size(d,2).

Roger Stafford

Re: FAST algorithm to jenga matrix?

Postby Bruno Luong » Mon, 30 Mar 2009 18:33:02 GMT

Here is a five-liner solution, but not much fundamentally different to Roger's solution:

A=[ 0 0 8 0 0 ; 
    1 4 0 0 2; 
    0 5 3 0 0; 
    7 0 0 6 0];

d = [2 6]'; % To be removed

[I J Val]=find(A);
f=ismember(unique(Val),d);
NewVal=cumsum(1-f);
NewVal(f)=0;
B=sparse(I,J,NewVal(Val),size(A,1),size(A,2));

% Alternatively, if no matrix duplicating is required
% then the last line changes to this

[I J Val]=find(A);
f=ismember(unique(Val),d);
NewVal=cumsum(1-f);
NewVal(f)=0;
A(sub2ind(size(A),I,J))=NewVal(Val);

% Bruno

Re: FAST algorithm to jenga matrix?

Postby Bruno Luong » Mon, 30 Mar 2009 19:32:45 GMT

Here is a four-liner solution. I could merge the 2nd and 3rd lines but it does not bring any advantage beside having less lines:

[I J Val]=find(A);
u = unique(Val); % 1:n
[f NewVal]=ismember(u,setdiff(u,d));
B=sparse(I,J,NewVal(Val),size(A,1),size(A,2));

% Bruno

Re: FAST algorithm to jenga matrix?

Postby Bruno Luong » Mon, 30 Mar 2009 19:48:01 GMT

For fun, two-liner solution:

[f NewVal]=ismember(1:max(nonzeros(A)),setdiff(1:max(nonzeros(A)),d));
A(A~=0)=NewVal(nonzeros(A));

Now how to get to one-liner solution (challenge proposed by Roger)?

Bruno

Re: FAST algorithm to jenga matrix?

Postby Bruno Luong » Mon, 30 Mar 2009 21:13:02 GMT

> 

A=[ 0 0 8 0 0 ; 
    1 4 0 0 2; 
    0 5 3 0 0; 
    7 0 0 6 0];
A=sparse(A)

d = [2 6]'; % To be removed

% One-liner solution !!!
A(A~=0) = subsref(subsasgn([],substruct('()',...
    {setdiff((1:max(nonzeros(A))),d)}), ...
     1:length(setdiff((1:max(nonzeros(A))),d))),...
     substruct('()',{nonzeros(A)}))

% Bruno

Re: FAST algorithm to jenga matrix?

Postby Bruno Luong » Mon, 30 Mar 2009 22:29:01 GMT

Sorry there is a bug in the previous code, it should be this:

% One-liner solution !!!
A(A~=0) = subsref(subsasgn(zeros(1,max(nonzeros(A))),substruct('()',...
    {setdiff((1:max(nonzeros(A))),d)}), ...
     1:length(setdiff((1:max(nonzeros(A))),d))),...
     substruct('()',{nonzeros(A)}))

% Bruno

Re: FAST algorithm to jenga matrix?

Postby Bruno Luong » Tue, 31 Mar 2009 00:35:01 GMT

% Another one-liner solution !!!
A(A~=0) = subsref(cumsum(~ismember(1:max(nonzeros(A)),d)).* ...
   ~ismember(1:max(nonzeros(A)),d),...
   substruct('()',{nonzeros(A)}))

% Bruno

Re: FAST algorithm to jenga matrix?

Postby Roger Stafford » Tue, 31 Mar 2009 01:22:01 GMT



  Sorry about the careless mistake, Hoi.  The third line should read:

 v = ones(size(v));

Roger Stafford

Re: FAST algorithm to jenga matrix?

Postby Hoi Wong » Tue, 31 Mar 2009 07:35:03 GMT



Thanks for your quick reply. I see a lot of ismember, unique, setdiff here which are famous for being slow. I'll set up a time test for all your solutions in a couple of days and see which one is the fastest :)

Re: FAST algorithm to jenga matrix?

Postby Hoi Wong » Tue, 31 Mar 2009 11:33:01 GMT





Thanks Roger! I've never given that much thought to the extended parameters for find(), sort() and a 5-argument use for sparse() though I've been using them for a while (sparse is quite new to me though. I'm developing a sparse_cell class/dataType and jengaMatrix is a piece of the puzzle)

My first algorithm was embarassing.......it's complicated, uses cellfun, sum, and runs 100000 times slower than any of yours, and it does not work for non-unique deletion list. Turns out that your 8-line solution is the fastest of all as I expected (because they are all built-in functions). Simple test is the matrix mentioned in my first post, and the second data set was captured from my project.

Here's the link to all of your algorithms and mine as well. jengaMatrixSpeedTest tests them all and generate the report below:

Does all algorithm give the same result for the simple test? true
---------------------------------------------------------------
Is the test matrices cardinal? true
Cardinality preserved? true
Is the deletion list unique? false
Roger: 0.00110589
Bruno1: 0.00870681
Bruno2: 0.02111930
Bruno4: 0.00462781
Bruno5: 0.00282878
Does all algorithm give the same result for the stress test (with non-unique deletion items)? true
Cardinality preserved? true
---------------------------------------------------------------
Hoi: 110.50091451
Roger: 0.00098781
Bruno1: 0.00648331
Bruno2: 0.00672234
Bruno4: 0.00160589
Bruno5: 0.00117171
Does all algorithm give the same result for the stress test (with unique deletion items)? true
Cardinality preserved? true 

Similar Threads:

1.Fast algorithm for generating doubly-stochastic matrices

Thanks a lot for your help.
Below is the way I finally implemented it (quite similar to your
ideas). It runs quite fast, and though it's hard to verify that the
matrices are indeed generated UAR, at least the plots show that all
the entries in the generated matrices are identically distributed.

Here's the simple code. Maybe I'll also upload it later as a shared
file.
% Simple algorithm for generating doubly-stochastic matrices
% (matrices, where the sum of each column and each row is exactly 1).
% The algorithm:
% 1. set an NxN matrix TM s.t TM[i,j] = 1/N for each 1<=i,j<=N.
% 2. for X number of iterations:
% 3. Draw i1, j1, i2, j2 UAR on [1,...,N].
% 4. Draw d UAR on (0, min {TM[i1, j1], TM[i2, j2]}).
% 5. M[i1,j1] <= M[i1,j1] - d;
% 6. M[i2,j2] <= M[i2,j2] - d;
% 7. M[i1,j2] <= M[i1,j2] + d;
% 8. M[i2,j1] <= M[i2,j1] + d;

% For a "good" distribution, X should be a (polynomial, probably)
function of N.
% I guess that X=N^4 is enough; for large N, maybe even a smaller X
is
% enough.

% The algorithm's code
% For make the generation more efficient, the code actually generates
many
% doubly-stochastic matrices "almost" at once.

% Constants
N = 4;
NUM_OF_SMPLS = 1000;
NUM_OF_BINS = 10;
BIN_INTERVAL = 1 / NUM_OF_BINS;

ar_of_TMs = repmat (ones (N) / N, [1 1 NUM_OF_SMPLS]);
for iter = 1: N^4
  indices = unidrnd (N, [NUM_OF_SMPLS 4]); % random indices i1, j1,
i2, j2 for all the sampled TMs
  i1 = indices (:, 1); j1 = indices (:, 2); i2 = indices (:, 3); j2 =
indices (:, 4);
  for smp_num = 1:NUM_OF_SMPLS
    % ar_of_max_delta (i) will hold min {TM(i1,j1), TM(i2,j2)} for
the i-th TM in the array; this is
    % the maximum allowed value for DELTA for this TM in the current
    % iteration
    ar_of_max_delta (smp_num) = min (ar_of_TMs (i1 (smp_num) ,
j1(smp_num), smp_num), ...
                                                                  
ar_of_TMs (i2 (smp_num) , j2(smp_num), smp_num));
  end; % for smp_num
  ar_of_delta = unifrnd (0, ar_of_max_delta); % Generate DELTA values
for all the TMs
  clear ar_of_max_delta; % not needed anymore - save memory space
  
  % add / substruct DELTA from the required entries in each TM
  for smp_num = 1:NUM_OF_SMPLS
    ar_of_TMs (i1 (smp_num), j1 (smp_num), smp_num) = ...
    ar_of_TMs (i1 (smp_num), j1 (smp_num), smp_num) - ar_of_delta
(smp_num);
    ar_of_TMs (i2 (smp_num), j2 (smp_num), smp_num) = ...
    ar_of_TMs (i2 (smp_num), j2 (smp_num), smp_num) - ar_of_delta
(smp_num);
    ar_of_TMs (i1 (smp_num), j2 (smp_num), smp_num) = ...
    ar_of_TMs (i1 (smp_num), j2 (smp_num), smp_num) + ar_of_delta
(smp_num);
    ar_of_TMs (i2 (smp_num), j1 (smp_num), smp_num) = ...
    ar_of_TMs (i2 (smp_num), j1 (smp_num), smp_num) + ar_of_delta
(smp_num);
  end; % for smp_num
  clear ar_of_delta;
  if (sum (sum (ar_of_TMs (:, :, smp_num)>0))~=N^2)
     error ('rgrgr');
  end;
end; % for iter

% An example of a plot for the PDFs: plot the PDFs of the entries in
the
% j-th col.
j = 1;
for i=1:N
  ar_of_TMij = zeros (NUM_OF_SMPLS, 1);
  ar_of_TMij (:) = ar_of_TMs (i, j, 1:NUM_OF_SMPLS);
  ar_of_bins = floor ( ar_of_TMij / BIN_INTERVAL + 1);
  for bin = 1:NUM_OF_BINS
    pdfer (bin, i) = size (find (ar_of_bins==bin), 1);
  end; % for bin
end; % for i
bar (pdfer);
legend ('Surface: bar PDF of the entries in a single row');

2.Point on Line Segment in 3D ? Fast Algorithm

Hello,

Im looking for a fast algorithm to determine if a point (3D) is inside a 
line segment (3D).
Can you help me ?

Regards

Jens



3.fast algorithm for detecting all Intersections of multiple linesegments

Hello,

Im looking for a fast  algorithm for detecting all Intersections of multiple
linesegments.(only linesegments nothing else)
In 2D there is a fast sweep line algorithm by Bentley-Ottmann but I did not
found a fast alogorithm for the 3D space.
Can you help me ?

regards
jens


4.Need to make the implemented SSD algorithm code faster

Hi!
I have implemented the SSD algorithm for matching correspondences for a rectified stereo pair. It works fine except that that the computation time takes several minutes to execute.

Below is the code I am using:

Il=imread('left_image','jpg');
Il=imread('right_image','jpg');
Il=double(Il);
Ir=double(Ir);
[R, C] = size(Il);
disp=zeros(R,C);

% rad is the radius of the correlation window
for i=rad+1:(R-rad) % search for all pixels in the 
for j=rad+1:(C-rad) % image, excluding boundaries
    
  % search along row for the matching pixel
  % k is the shift in column index along a particular row
    Iwl = Il(i-rad:i+rad, j-rad:j+rad);
    for k=0:(C-(2*rad)-1)
        Iwr = Ir(i-rad:i+rad, 1+k:1+(2*rad)+k);
        SSD(1,k+1) = sum(sum((Iwl-Iwr).^2))
    end
   %find the index which gives minimum SSD
   match = find(SSD==min(SSD))  
   %reset the SSD values to zero for the next row 
   SSD=zeros(1,C-2*rad);
end
end

What I really need is some way of making the SSD calculation faster.  I have tried to use imfilter, conv2 to eliminate at least one loop, but I could not get good results.

Thank you very much,
Tiziana

5.Problem with Fast Fourier Transform algorithm

Why don't you use Matlab's own function 'fft.m'?

-- 
Dr. Lasse Murtomi
Helsinki University of Technology
 XXXX@XXXXX.COM 


6. Fast 2D Regular Grid traversal algorithm

7. fast algorithm routine for atan2 with reduced accuracy

8. Are fast algorithms available in computing HyperGeometric functions?



Return to matlab

 

Who is online

Users browsing this forum: No registered users and 98 guest