How can i get sub matrices with using for loop
7 次查看(过去 30 天)
显示 更早的评论
Hello everyone
I have a matrix A as:
A=[1
1
1
1
1
1
1
1
1
1
2
2
2
2
2
2
2
2
2
2
3
3
3
3
3
4
4
4
4
4
5
5
5
5
5
6
6
6
6
6
7
7
7
7
7
7
7
7
7
7
8
8
8
8
8
8
8
8
8
8
9
9
9
9
9
9
9
9
9
9
10
10
10
10
10
10
10
10
10
10
11
11
11
11
11
11
11
11
11
11
12
12
12
12
12
12
12
12
12
12]
I get unique(A) as 12. And then i want to get new matrices as
for i=1, A(1) as row which is equal to i. And for i=2, A(2) as row which is equal to i.............for i=12 A(12) as rows which is equal to i.
For example:
A(1)=[1
1
1
1
1
1
1
1
1
1]
A(2)=[2
2
2
2
2
2
2
2
2
2]
A(3)=[3
3
3
3
3]
........
A(12)=[12
12
12
12
12
12
12
12
12
12]
How can i do this with using for loop or another way? I want to get new matrices as A(1) to A(12).
0 个评论
采纳的回答
Kevin Phung
2019-9-16
编辑:Kevin Phung
2019-9-16
you can store each unique value in a cell array:
A_unique = unique(A);
A2 = cell(size(A_unique))
for i = 1:numel(A_unique)
A2{i} = A(A==A_unique(i))
end
then simple call out:
A{1}
A{2}
A{3}
%... etc
2 个评论
Kevin Phung
2019-9-18
编辑:Kevin Phung
2019-9-18
for i=1:12
B{i}=(i+3).*A{i}.^2
end
you need a dot (.) before your multiplication and exponent operator.
更多回答(1 个)
madhan ravi
2019-9-17
a=arrayfun(@(x)A(A==unique(x)),A,'un',0)
celldisp(a)
If you’re goal is simply to create A with repetitions of 10 then it’s simply:
A = num2cell(repmat(1:12,10,1),1)
celldisp(A)
3 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!