Q:

Pairing up and iterating through values from two CSV files

I currently have two, two column matrices (each roughly 2 x ~50000 double) which were both read from data generated in separate programs. 
The first matrix, called csv_matrix_sorted contains node IDs in the first column (in ascending order) and product IDs in the second column.  The node IDs are not repeated, but the product IDs vary between 1 and 100 for every node. 
The second matrix, called stress_matrix_sorted also contains node IDs in the first column (in ascending order), and the corresponding stress value in the second column. 
My goal is to get the maximum stress value for every product ID.  
One small issue I've run into is that while the matrix dimensions are almost identical (46413 vs. 46400 rows), every once in a while there seems to be stray node that's not accounted for - this means that I also need to check whether a node has a corresponding stress value, and only then pair it up with the product ID. 
So far I've tried:
damage_column = [];
for i = 1:length(stress_matrix_sorted)
    if stress_matrix_sorted(i,1) == csv_matrix_sorted(i,1)
        damage_column = [damage_column, csv_matrix_sorted(i,2)];
    end
end

The problem is that this code stops writing to damage_column once the node IDs are no longer exactly aligned, as expected.  My initial thought was to append damage_column to the damage list, and then try to loop through the product IDs, but I'm not sure that's the best approach.  I'm open to any feedback! (: 

A:

There are at least two choices.  One is what you suggest of looping through the products (but this does not take advantage of the two matrices being already sorted):
nProd = 100; % how many products you have
maxStressMatrix = zeros(nProd,1); % one row for each product. 
for p = 1:nProd
% ..This gets the indices of product 'p' in the first matrix
    prIdx = csv_matrix_sorted(:,2) == p; % Warning: prIdx has as many rows as csv_matrix_sorted 
    if isempty(prIdx) % skip the rest if no node has product 'p'
        continue
    end
    maxStrs = -inf;
% ..This gets the nodes that contain product 'p'
    node_list = csv_matrix_sorted(prIdx,1);
    for n = 1:numel(node_list)
        node = node_list(n); 
    % ..Find the node in the other matrix
        k = find(stress_matrix_sorted(:,1) == node);
        if isempty(k) % skip the rest if node is not in the stress mtx. 
            continue
        end
        maxStrs = max(maxStrs,stress_matrix_sorted(k,2));
    end
    maxStressMatrix(p) = maxStrs;
end

A second approach is to use two indices, one for each matrix.  When they point to the same node, do your calculations and advance both together.  When they don't, advance the one with the smaller node number till it catches with the other one.  At each step check that neither index exceeds the size of the matrix:
maxStressMatrix = -inf(nProd,1); % one row for each product
csvIdx = 1; % index for csv_matrix_sorted 
strsIdx = 1; % index for stress_matrix_sorted
% Numer of nodes in each matrix
Ncsv = size(csv_matrix_sorted, 1); 
Nstrs = size(stress_matrix_sorted, 1);

while true
% ..When either index exceeds the matrix size, exit the loop
    if csvIdx > Ncsv || strsIdx > Nstrs
        break
    end
% ..Get the nodes from each matrix
    csvNode = csv_matrix_sorted(csvIdx,1);
    strsNode = stress_matrix_sorted(strsIdx,1);
% ..If the nodes are not equal, advance the index of the one with the
%   smaller node ID and repeat the loop 
    if csvNode < strsNode 
        csvIdx = csvIdx + 1;
        continue
    end
    if strsNode < csvNode
        strsIdx = strsIdx+1;
        continue
    end
% ..Here, the two node IDs are equal
    p = csv_matrix_sorted(csvIdx, 2); % this is the product
    s = stress_matrix_sorted(strsIdx, 2);  % the corresponding stress
    maxStressMatrix(p) = max(maxStressMatrix(p), s); % update maximum
% ..Since the nodes are equal, increment both indices
    csvIdx = csvIdx + 1;
    strsIdx = strsIdx + 1;
end

If a product ends with maxStress = -Inf, that means the product was not found in at least one of the two matrices. 
HTH

