Info
This question is closed. Reopen it to edit or answer.
Separating rows by attribute into new matrices.
1 view (last 30 days)
Show older comments
Hi there,
I'm hoping someone can help me with this seemingly simple objective. I have a bunch of CIELAB values stored in a 1493x3 matrix like this:
75.0762811831022 5.18089489085732 14.6428639876497
73.5614856255498 5.30582608119210 14.6982379202033
69.3445535098640 5.14055617598219 13.5092813281339
79.4934199520990 4.66394941442407 11.9900551014935
79.0393320098919 4.36754772094482 11.5537506643861
I'd like to have a bunch of new matrices that store each of these rows by their first value (L*-value), lets say new matrices which contain all three rows for values with L*79, L*75, L*73 ect.
So in this case I would end up with 4 new matrices for L*-values of 75, 73, 69 and 79 and each of these matrices contains the entire row.
Your help is much apreciated.
Regards
SH
0 Comments
Answers (1)
drummer
on 31 Mar 2020
Hi,
Would it work if instead of several matrices, you get a new matrix with a third index?
So each index of the third dimension is the a new row of your previous input matrix.
As in your example, you provide:
75.0762811831022 5.18089489085732 14.6428639876497
73.5614856255498 5.30582608119210 14.6982379202033
69.3445535098640 5.14055617598219 13.5092813281339
79.4934199520990 4.66394941442407 11.9900551014935
79.0393320098919 4.36754772094482 11.5537506643861
What I mean is, instead of
m1 = [75.07 , 5.18, 14.64];
m2 = [73.56, 5.30, 14,.69]; and so on, you'd rather have
newMatrix(1, :, 1) = [75.07 , 5.18, 14.64];
newMatrix(2, :, 2)= [[73.56, 5.30, 14,.69];
newMatrix = zeros(1,3,1493);
yourMatrix = rand(1493,3);
[rowLen, colLen] = size(yourMatrix);
for z = 1 : rowLen % creates indexes for the 3rd dimension of newMatrix
for i = 1 : rowLen % reads your rows
for j = 1 : colLen % reads your cols
newMatrix(i,j,z) = squeeze(yourMatrix(i,j,:));
end
end
end
Cheers
3 Comments
drummer
on 31 Mar 2020
I tested it before posting. You should check the rows by doing this in the command line:
% checking the values in a single row
yourMatrix(1,:) % This shows the random values in the 1st row, from yourMatrix = rand(1493,3)
To see if newMatrix has the first row separated in newMatrix(1,:,1), type in the command window:
newMatrix(1,:,1) % This should show the same values as in the previous line.
Do not use ; so you can se the outputs.
These outputs should be the same, so you have your vectors separated in newMatrix
FYI: If you want to change the code to your input, adjust the matrix sizes accordingly. Not sure if it's a typo, but you previously wrote 1493, and now 14983. The code was just an example to your approach.
In another topic: In your sample matrix you had 5 rows, but you wanted 4. Which of both rows with 79 you should have?
If you want a smaller number of vectors, you must add a comparison step after the loop to find the values you want. Otherwise, # of 'separated' vectors = # of rows of your input matrix.
Cheers
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!