Separating rows by attribute into new matrices.

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

답변 (1개)

drummer
drummer 2020년 3월 31일

0 개 추천

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

Hi @drummer - thanks for your help mate! Unfortunately that doesn't do it. I ran your sript and get a bunch of zeros. Remember that I have a really large array here (14983x3). The max value for L* is 85.5 and min for L* is 41.8 so I would need 41 arrays which contain all respective LAB values...
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
Hi @drumner!
Thanks again for your help. I probably failed to explain properly what I'm trying to do - my appologies. So I have an array of 14983x3 (sorry for the previous typo) and these are L*a*b*-values. So for instance 75.07 , 5.18, 14.64 referrs to L*75.07 a*5.18 and b*14.64. This array that I have subsequently has 14983 L*values ranging from L*41 to L*84. What I need is 43 arrays from 41 all the way up to 84. Each of these should contain all L*a*b*-values for each of these 43 L*-groups.
For instance I can simply use:
x=LAB(LAB(:, 1)<61, :);
L60=x(x(:, 1)>60, :);
LAB is my 14983x3 array and these two lines will create a new array called "L60" which contains all L*a*b*-values with L*60:
60.8008798471768 4.85292890948374 11.1145675710373
60.7213587044256 10.2993584240051 17.7875693024272
60.5630488563623 14.5555978316287 38.0850677656340
60.4203311933741 6.82778759593458 12.8733422501348
60.3948687072869 6.49893315895765 14.4663606974790
60.3287778519643 7.02156654492907 12.2648732211747
60.3040045656528 8.68237804846326 19.4577309730639
60.2968866679374 11.0826197879960 19.5396305850156

이 질문은 마감되었습니다.

제품

릴리스

R2019a

질문:

2020년 3월 31일

마감:

2021년 8월 20일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by