The matrix has the form:
0.1, 1, 0.2, 0.3, 0.4, 0.50.1, 1, 0.2, 0.3, 0.4, 0.50.1, 2, 0.2, 0.3, 0.4, 0.50.1, 2, 0.2, 0.3, 0.4, 0.50.1, 2, 0.2, 0.3, 0.4, 0.50.1, 2, 0.2, 0.3, 0.4, 0.50.1, 3, 0.2, 0.3, 0.4, 0.50.1, 3, 0.2, 0.3, 0.4, 0.50.1, 3, 0.2, 0.3, 0.4, 0.50.1, 4, 0.2, 0.3, 0.4, 0.5
I want to create an array with all rows that have the same element for the second column, into a different layer. For example:
Array(:, :, 2) should = [0.1, 2, 0.2, 0.3, 0.4, 0.50.1, 2, 0.2, 0.3, 0.4, 0.50.1, 2, 0.2, 0.3, 0.4, 0.50.1, 2, 0.2, 0.3, 0.4, 0.5]; and Array(:, :, 4) should = [0.1, 4, 0.2, 0.3, 0.4, 0.5];
However, I have 100,000s of rows in the matrix and so have to be as efficient as possible (and so not iterate through all rows of the matrix more than once). I am trying to understand how to approach this problem conceptually. I tried several approaches with my novice experience handling MATLAB. I tried using bsxfun(@eq…) in a loop from 1:4 to find the rows whose second column equal the number being iterated. I tried to get the number of rows with each number into an array (‘array_number_of_rows’) (example: there are 4 rows with ‘2’ in the second column above) and then using this to trying to create an array. Example:
array_number_of_rows = [1 22 43 34 1];
Then I’d loop from 1:4, trying to assign rows from the matrix:
count = 1;for i=1:4 Array(:, :, i) = [matrix(count: array_number_of_rows(i, 2), :]; count = count + array_number_of_rows(i, 2);end
(note: this isn’t the actual code I used) I think that the problem is that the number of rows for each number isn’t the same.
I also tried using the Boolean values from bsxfun as indices for the matrix and failed horribly (dimension issues again – I tried to counter this by using logical(), and filling with ones/zeroes and even by reshaping the matrices in the loop).
After several hours, I ultimately figured that I've got my core understanding of the problem wrong. Would someone mind clarifying whether my approaches were in the right direction, or whether I am missing some simple solution to the problem? Thank you for your time!
Best Answer