How may I split a matrix into two based on 0/1 of the first column?

3 次查看(过去 30 天)
M = [0 1 3
0 2 2
0 3 5
1 1 4
1 2 9
1 3 8
1 4 5
1 5 3
0 1 2
0 2 1
1 1 7
1 2 6
1 3 3
1 4 8
0 1 7
0 2 4
0 3 7];
Hi! Say I have this matrix M, and I want to make two matrices based on M. M0 is the part of M where the first column is 0, and M1 is where the first column is 1. M0 and M1 should look like this:
M0 = [1 3
2 2
3 5
1 2
2 1
1 7
2 4
3 7];
M1 = [1 4
2 9
3 8
4 5
5 3
1 7
2 6
3 3
4 8]
Could someone please tell me how to get M0 and M1? Thanks a lot!

采纳的回答

the cyclist
the cyclist 2023-2-11
编辑:the cyclist 2023-2-11
M = [0 1 3
0 2 2
0 3 5
1 1 4
1 2 9
1 3 8
1 4 5
1 5 3
0 1 2
0 2 1
1 1 7
1 2 6
1 3 3
1 4 8
0 1 7
0 2 4
0 3 7];
M1 = M(M(:,1)==0,2:3)
M1 = 8×2
1 3 2 2 3 5 1 2 2 1 1 7 2 4 3 7
M2 = M(M(:,1)==1,2:3)
M2 = 9×2
1 4 2 9 3 8 4 5 5 3 1 7 2 6 3 3 4 8
  6 个评论
Walter Roberson
Walter Roberson 2023-2-11
In my opinion, your variables M0 and M1 were named very directly: M0 being associated with condition 0, and M1 being associated with condition 1. For such a small number of variables, I think this is pretty clear and not harmful at all.
But on the other hand, using M1 and M2 gets into the territory of using numbered variables. "First M" and "Second M". Those do not give any useful information to the readers about what they mean, and if you are going to use that then it probably makes more sense in program construction to use a cell array M{1} M{2}
I am not as against "encoding data in the variable name" as some of the volunteers are. For example, as far as I am concerned,
R = Img(:,:,1);
G = Img(:,:,2);
B = Img(:,:,3);
can be classified as "encoding data in the variable name".
The boundary between acceptable and not recommended is a bit fuzzy for me. Names that directly tell what they are talking about are generally welcome, but if you get "too many" of them then there is probably a better arrangement.
For example you might have reason to group data according to three bits, and in such a case using variable names M000 M001 M010 M011 M100 M101 M110 M111 would be direct representation and probably reasonable to work with. But if you needed to use (for example) 8 bits, M00000000 M00000001 through M11111111 then that is probably too much. At the moment I feel as if 4 bits, M0000 through M1111 would be about the upper reasonable limit (unless machine coding was being used); I might feel differently later.
Julia
Julia 2023-2-12
Thank you @Walter Roberson! I think I am starting to understand this problem about naming variables.

请先登录,再进行评论。

更多回答(1 个)

John D'Errico
John D'Errico 2023-2-11
If you will split it into only two matrices, then use named variables as you wish. If you wanted to split it into multiple parts, based on that first index, I would split it into a cell array.
M = [0 1 3
0 2 2
0 3 5
1 1 4
1 2 9
1 3 8
1 4 5
1 5 3
0 1 2
0 2 1
1 1 7
1 2 6
1 3 3
1 4 8
0 1 7
0 2 4
0 3 7];
M0 = M(M(:,1) == 0,2:3)
M0 = 8×2
1 3 2 2 3 5 1 2 2 1 1 7 2 4 3 7
M1 = M(M(:,1) == 1,2:3)
M1 = 9×2
1 4 2 9 3 8 4 5 5 3 1 7 2 6 3 3 4 8
  2 个评论
Julia
Julia 2023-2-11
Thank you so much for your advice!
Do you mind explain further why using cell arrays would be more appropriate for multiple parts? I read the MATLAB description about cell array and indexing, but still feel confused...
Dyuman Joshi
Dyuman Joshi 2023-2-12
Suppose you have to calculate M0, M1, M2, ..., M8.
You can do it like above, calculating each variable individually. That is not recommended. You can find the reasoning as to why not, in the link mentioned @the cyclist's answer.
Instead one should use indexing and store the results in cell array as John mentioned above.
Why Cell arrays you might ask?
Because corresponding to every index, the size of each resultant matrix might be different and cell arrays are perfect to store such data.
%modified data
M = [0 1 3;
8 2 2
2 3 5
4 1 4
3 2 9
7 3 8
5 4 5
1 5 3
5 1 2
0 2 1
8 1 7
1 2 6
6 3 3
2 4 8
4 1 7
6 2 4
0 3 7];
%0 to 8, 9 numbers
n=9;
%pre-allocation to boost up speed for large calculations
out=cell(1,n);
%using loop to index
for k=1:n
idx=M(:,1)==(k-1);
out{k}=M(idx,2:end);
end
%here out{1} will correspond to M0, out{2} to M1, out{9} to M8
%as indexing in MATLAB starts with 1
out
out = 1×9 cell array
{3×2 double} {2×2 double} {2×2 double} {[2 9]} {2×2 double} {2×2 double} {2×2 double} {[3 8]} {2×2 double}
out{1} %M0 - 1st, 10th and last row
ans = 3×2
1 3 2 1 3 7
out{4} %M3 - 5th row
ans = 1×2
2 9
out{7} %M6 - 13th and 16th row
ans = 2×2
3 3 2 4
As I mentioned above, you can see that the size of these cell elements are different.
Indexing is simple, not just to read or write, but to implement as well and incredibly efficient.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by