MATLAB Answers

How can i get sub matrices with using for loop

3 views (last 30 days)
Mooner Land
Mooner Land on 16 Sep 2019
Commented: Mooner Land on 18 Sep 2019
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 Comments

Sign in to comment.

Accepted Answer

Kevin Phung
Kevin Phung on 16 Sep 2019
Edited: Kevin Phung on 16 Sep 2019
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 Comments

Mooner Land
Mooner Land on 17 Sep 2019
Thank you friend, i can already get something like this.
I wan to run a code for every seperate A{i} matrix.
something like this
for i=1:12
B{i}=(i+3)*A{i}^2
end
This is a simplicated code as an example. I have a bigger code to run for every seperate matrix. I hope i could tell my problem.
Thank you.
Kevin Phung
Kevin Phung on 18 Sep 2019
for i=1:12
B{i}=(i+3).*A{i}.^2
end
you need a dot (.) before your multiplication and exponent operator.

Sign in to comment.

More Answers (1)

madhan ravi
madhan ravi on 17 Sep 2019
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 Comments

Mooner Land
Mooner Land on 17 Sep 2019
Thank you friend, i can already get something like this.
I wan to run a code for every seperate A{i} matrix.
something like this
for i=1:12
B{i}=(i+3)*A{i}^2
end
This is a simplicated code as an example. I have a bigger code to run for every seperate matrix. I hope i could tell my problem.
Thank you.
madhan ravi
madhan ravi on 18 Sep 2019
How's this related to the question you originally asked?
Mooner Land
Mooner Land on 18 Sep 2019
I want to get every seperate matrix for come calculations but i cant split matrix to 12 seperate matrix.

Sign in to comment.

Sign in to answer this question.