Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7561905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:16:46+00:00 2026-05-30T13:16:46+00:00

In MATLAB, I have a for loop which has a lot of interations to

  • 0

In MATLAB, I have a for loop which has a lot of interations to go through and fill a sparse matrix. The program is very slow and I would like to optimize it to see it finish some time soon. In two lines I use the command find, and the editor of MATLAB, warns me that the use of logical indexing instead of find will improve performace. My code is quite similar to that presented to the mathworks newreader, mathworks newsreader recommendation, where there is a vector of values and a vector of unique value generated from it. Uses find to obtain the index in the unique values (for updating the values in a matrix). To be brief, the code given is:

     positions = find(X0_outputs == unique_outputs(j,1));
% should read
     positions = X0_outputs == unique_outputs(j,1);

But the last line is not the index, but a vector of zeros and ones.
I have an illustrative example, make a set of indices; tt=round(rand(1,6)*10):

 tt = 3     7     1     7     1     7

Make a unique vector; ttUNI=unique(tt)

ttUNI = 1     3     7

Use find to get the position index of the value in the set of unique values; find(ttUNI(:) == tt(1))

ans = 2

Compare with using logical indexing; (ttUNI(:) == tt(1))

ans =
 0
 1
 0

Having the value 2 is alot more useful than that binary vector when I need to update the indices for a matrix. For my matrix, I can say mat(find(ttUNI(:) == tt(1)), 4) and that works. Whereas using (ttUNI(:) == tt(1)) needs post processing.

Is there a neat and efficient way of doing what is needed? Or is the use of find unavoidable in circumstances such as these?

UPDATE: I will include code here as recommended by user: @Jonas to give better insight into the problem which I am having and report some of the profiler tool’s results.

ALL_NODES = horzcat(network(:,1)',network(:,2)');
NUM_UNIQUE = unique(ALL_NODES);%unique and sorted    
UNIQUE_LENGTH = length(NUM_UNIQUE);
TIME_MAX = max(network(:,3));
WEEK_NUM = floor((((TIME_MAX/60)/60)/24)/7);%divide seconds for minutes, for hours, for days and how many weeks
%initialize tensor of temporal networks
temp = length(NUM_UNIQUE);
%making the tensor a sparse 2D tensor!!! So each week is another replica of
%the matrix below
Atensor = sparse(length(NUM_UNIQUE)*WEEK_NUM,length(NUM_UNIQUE));
WEEK_SECONDS = 60*60*24*7;%number of seconds in a week

for ii=1:size(network,1)%go through all rows/observations 
    WEEK_NOW = floor(network(ii,3)/WEEK_SECONDS) + 1;
    if(WEEK_NOW > WEEK_NUM)
        disp('end of weeks')
        break
    end
    data_node_i = network(ii,1);
    Atensor_row_num = find(NUM_UNIQUE(:) == data_node_i)...
        + (WEEK_NOW-1)*UNIQUE_LENGTH;
    data_node_j = network(ii,2);
    Atensor_col_num = find(NUM_UNIQUE(:) == data_node_j);
    %Atensor is sparse
    Atensor(Atensor_row_num,Atensor_col_num) = 1;          
end

Here UNIQUE_LENGTH = 223482 and size(network,1)=273209. I rand the profiler tool for a few minutes, which was not enough time needed for the program to finish, but to reach a steady state when the ratio of times would not change too much. Atensor_row_num = find(NUM_UNI.. is 45.6% and Atensor_col_num = find(NUM_UNI... is 43.4%. The line with Atensor(Atensor_row_num,Atenso... which allocates values to the sparse matrix, is only 8.9%. The length of the NUM_UNIQUE vector is quite large, so find is an important aspect of the code; even more important than the sparse matrix manipulation. Any improvement here would be significant. I don’t know if there is a more efficient logical progression for this algorithm to proceed as well rather than taking the straightforward approach of replacing find.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:16:48+00:00Added an answer on May 30, 2026 at 1:16 pm

    find is indeed unavoidable under certain circumstances. For example, if you want to loop over indices, i.e.

    idx = find(someCondition);
    for i = idx(:)'
        doSomething
    end
    

    or if you want to do multi-level indexing

    A = [1:4,NaN,6:10];
    goodA = find(isfinite(A));
    everyOtherGoodEntry = A(goodA(1:2:end));
    

    or if you want the first n good values

    A = A(find(isfinite(A),n,'first');
    

    In your case, you may be able to avoid the call to find by using the additional outputs of unique

    [uniqueElements,indexIntoA,indexIntoUniqueElements] = unique(A);
    

    Before you try to optimize your code by fixing what you think takes time, I suggest you run the profiler on your code to check what really takes time. And then you can possibly post the code of your actual loop, and we may be able to help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Matlab, I have a loop that performs operations on arrays. I would like
I have an NxM matrix in MATLAB that I would like to reorder in
I have a matrix in MATLAB from which I want to sample every other
I have a matrix, matrix_logical(50000,100000) , that is a sparse logical matrix (a lot
I have an RGB image in MATLAB, and I want to loop through each
The problem with octave(matlab). In the program I have loop where I plot data.
I have a Matlab program that generates a list x = 6.1692 8.1863 5.8092
I have a Matlab program that is running longer than I'd like it to.
I'd like to have a MATLAB array fill a column with numbers in increments
I am generating an Excel file through MATLAB and I have empty cells in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.