Concatenate every n-th row - matlab

I got a data set in a matrix like the following (imported from Excel):
matrix =
Cat1 1 2 3 4
Cat2 9 10 11 12
Cat3 17 18 19 20
Cat1 5 6 7 8
Cat2 13 14 15 16
Cat3 21 22 23 24
I would like to reshape it into 3 vectors (one for every category) of the same size to do a stacked bar plot. Vectors should look like this after reshape operation (It would be nice if the vector had the name of the first column and the matrix could be of any size):
cat1 = [ 1 2 3 4 5 6 7 8]
cat2 = [ 9 10 11 12 13 14 15 16]
cat3 = [17 18 19 20 21 22 23 24]
I sincerely hope this is not duplicate. I couldn't produce a working solution with the help of the other reshape questions.

If your data is a matrix, you can manipulate the order of the rows when indexing, so you can do something like this:
rows = reshape(1:size(matrix, 1), n, []).';
res = reshape(matrix(rows, :).', [], n).';
The resulting matrix res is composed of the concatenated rows.
This solution holds for cell arrays as well, but you'll need an additional cell2mat to turn the result into a matrix.
Example
matrix = [1:4; 9:12; 17:20; 5:8; 13:16; 21:24];
n = 3;
rows = reshape(1:size(matrix, 1), n, []).';
res = reshape(matrix(rows, :).', [], n).';
The result is:
res =
1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24

EDIT:
Try the following:
%# dataset stored in a cell array
data = {
'Cat1' 1 2 3 4
'Cat2' 9 10 11 12
'Cat3' 17 18 19 20
'Cat1' 5 6 7 8
'Cat2' 13 14 15 16
'Cat3' 21 22 23 24
};
%# get all possible values of first column,
%# and map them to integer indices
[L,~,IDX] = unique(data(:,1));
%# for each possible "category"
groups = cell(max(IDX),1);
for i=1:max(IDX)
%# get the rows of numeric data matching current category
M = data(IDX==i, 2:end)';
%# flatten matrix into a vector and store in cell (row-major order)
groups{i} = [M{:}];
end
Now you can access the i-th "cat" vector as: groups{i}
>> [cat1,cat2,cat3] = deal(groups{:})
cat1 =
1 2 3 4 5 6 7 8
cat2 =
9 10 11 12 13 14 15 16
cat3 =
17 18 19 20 21 22 23 24
Note that the matching "cat" labels are stored in L{i} (the mapping keys)

Related

Sorting every layer of 3D matrix by one column each

I have a 3D matrix. Say it is:A = randi(15,[4,3,2]). I want to sort the 2nd column of each layer in an ascending order, but the other columns simply stayed in their respective rows. How can I do that?
If the two layers are like this
val(:,:,1) =
6 12 13
10 14 8
15 8 2
4 3 14
val(:,:,2) =
10 1 8
2 15 12
14 11 1
1 6 11
Then I want a result like this
val(:,:,1) =
4 3 14
15 8 2
6 12 13
10 14 8
val(:,:,2) =
10 1 8
1 6 11
14 11 1
2 15 12
If you have the Image Processing Toolbox, using blockproc is one solution:
val(:,:,1) = [ ...
6 12 13
10 14 8
15 8 2
4 3 14]
val(:,:,2) = [ ...
10 1 8
2 15 12
14 11 1
1 6 11]
%// row indices to used for sorting
rowidx = 2;
[n,m,p] = size( val );
%// get a 2D matrix
val2D = reshape(val, n, [], 1)
%// sorting
out2D = blockproc(val2D,[n,m],#(x) sortrows(x.data,rowidx))
%// transform back to 3D
out3D = reshape(out2D, n, m, [])
Without the toolbox, maybe a little slower:
temp = arrayfun(#(x) sortrows(val(:,:,x),rowidx),1:size(val,3),'uni',0)
out3D = cat(3,temp{:})
out3D(:,:,1) =
4 3 14
15 8 2
6 12 13
10 14 8
out3D(:,:,2) =
10 1 8
1 6 11
14 11 1
2 15 12

How to select different row in a matrix in Matlab every time?

I want to create a matrix which has distinct rows selected from another matrix.
For Example, I have a 10x3 matrix A
A =
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17 18
19 20 21
22 23 24
25 26 27
28 29 30
Now I want to create a new matrix B of size 2 X 3 from A in a iterative process in such a way that the matrix B should consist different rows in each iteration (max iteration = 5)
My Pseudo-code:
for j=1:5
create matrix 'B' by selecting 2 rows randomly from 'A', which should be different
end
You could use randperm to mess up the rows randomly and then take two rows in each iteration successively in order.
iterations = 4;
permu = randperm(size(A,1));
out = A(permu(1:iterations*2),:);
for ii = 1:iterations
B = out(2*ii - 1:2*ii,:)
end
Results:
B =
22 23 24
25 26 27
B =
1 2 3
13 14 15
B =
19 20 21
16 17 18
B =
7 8 9
10 11 12

Matlab: Cut Vector at missing values and create new vectors

I want to write a Matlab script.
In my example I have a vector A=[1 3 4 5 7 8 9 10 11 13 14 15 16 17 19 20 21]
Now I want to cut the vector automatically at the points where a number is missing(here the numbers 2, 6, 12, 18 are missing).
As a result I want to have the vectors [1] and [3 4 5] and [7 8 9 10 11] and [13 14 15 16 17] and [19 20 21]. So as you can see the new vectors have different lenghts.
I thought about using a for loop, but I am not sure how to write these new vectors.
Thank you for your help :)
One liner with diff, cumsum & accumarray -
out = accumarray(cumsum([0 ; diff(A(:))~=1])+1,A(:),[],#(x) {x})
Sample run -
>> A
A =
1 3 4 5 7 8 9 10 ...
11 13 14 15 16 17 19 20 21
>> celldisp(out)
out{1} =
1
out{2} =
3
4
5
out{3} =
7
8
9
10
11
out{4} =
13
14
15
16
17
out{5} =
19
20
21
This is one approach:
s = [find(diff(A(:).')>1) numel(A)]; %'// detect where consecutive difference exceeds 1
s = [s(1) diff(s)]; %// sizes of groups
result = mat2cell(A(:).', 1, s); %'// split into cells according to those sizes
In your example, this gives
>> celldisp(result)
result{1} =
1
result{2} =
3 4 5
result{3} =
7 8 9 10 11
result{4} =
13 14 15 16 17
result{5} =
19 20 21
Another approach (computes group sizes differently):
s = diff([0 sum(bsxfun(#lt, A(:), setdiff(1:max(A(:).'), A(:).')), 1) numel(A)]);
result = mat2cell(A(:).', 1, s);

subset matrix using find function in Matlab

I made a matrix in Matlab, say,
A = magic(5);
A =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
Now I found the indices I want using the find function as:
ind = find(A(:,5)>3 & A(:,4)>= 8);
ind =
1
2
3
Now if I want to get a subset of matrix A for those indices using B = A(ind) function, I only get the first column of the matrix:
B = A(ind)
B =
17
23
4
How can I get all the columns as subset??
Oops ... I got it
B = A(ind,:);

Sorting a vector by the number of time each value occurs

We have the following case:
Q = [idxcell{:,1}];
Sort = sort(Q,'descend')
Sort =
Columns 1 through 13
23 23 22 22 20 19 18 18 18 18 17 17 17
Columns 14 through 26
15 15 14 14 13 13 13 12 12 12 11 10 9
Columns 27 through 39
9 9 8 8 8 8 8 7 7 7 7 7 7
Columns 40 through 52
7 6 6 6 5 4 4 3 3 3 3 2 2
Columns 53 through 64
2 2 2 2 2 2 2 1 1 1 1 1
How can we sort matrix Sort according to how many times its values are repeated?
Awaiting result should be:
repeatedSort = 2(9) 7(7) 1(5) 8(5) 3(4) 18(4) 6(3) 9(3) 12(3) 13(3) 17(3) 4(2) 14(2) 15(2) 22(2) 23(2) 5(1) 10(1) 11(1) 19(1) 20(1)
or
repeatedSort = 2 7 1 8 3 18 6 9 12 13 17 4 14 15 22 23 5 10 11 19 20
Thank you in advance.
You can use the TABULATE function from the Statistics Toolbox, then call SORTROWS to sort by the frequency.
Example:
x = randi(10, [20 1]); %# random values
t = tabulate(x); %# unique values and counts
t = t(find(t(:,2)),1:2); %# get rid of entries with zero count
t = sortrows(t, -2) %# sort according to frequency
the result, where first column are the unique values, second is their count:
t =
2 4 %# value 2 appeared four times
5 4 %# etc...
1 3
8 3
7 2
9 2
4 1
6 1
Here's one way of doing it:
d = randi(10,1,30); %Some fake data
n = histc(d,1:10);
[y,ii] = sort(n,'descend');
disp(ii) % ii is now sorted according to frequency