In MatLab 2016b, is there a way to group the values of the below vector by non-descending order?
values = [1 1 1 7 17 74 89 91 96 1 5 32 43 78 84 95 98 100 0 0 15 31 69 88 94 97 100 100 100 0 2 12 42 66 78 83 89 94 97 1 6 34 63 65 75 89 93 98]
so I will get the following groups:
group A) 1 1 1 7 17 74 89 91 96
group B) 1 5 32 43 78 84 95 98 100
group C) 0 0 15 31 69 88 94 97 100 100 100
group D) 0 2 12 42 66 78 83 89 94 97
group E) 1 6 34 63 65 75 89 93 98
The following code should give you what you are looking for:
% Define the values...
values = [1 1 1 7 17 74 89 91 96 1 5 32 43 78 84 95 98 100 0 0 15 31 69 88 94 97 100 100 100 0 2 12 42 66 78 83 89 94 97 1 6 34 63 65 75 89 93 98];
% Define the linear indices...
subs = 1:numel(values);
% Define the grouping indices...
idx = [0 subs(diff(values) < 0) subs(end)];
% Split the values into multiple arrays...
result = arrayfun(#(k)values(idx(k-1)+1:idx(k)),2:numel(idx),'UniformOutput',false);
Here is the final output:
>> result{:}
ans =
1 1 1 7 17 74 89 91 96
ans =
1 5 32 43 78 84 95 98 100
ans =
0 0 15 31 69 88 94 97 100 100 100
ans =
0 2 12 42 66 78 83 89 94 97
ans =
1 6 34 63 65 75 89 93 98
diff(values)
returns a vector with the difference between each element of values and the following element. We can use this in a comparison to give a logical vector:
diff(values) < 0
returns a vector containing true, or 1, whenever the corresponding element of values is less than the following element. To finish the job we can use the cumsum function which returns a vector containing the running total of the elements in the input vector:
groups = cumsum([1, diff(values) < 0])
Just one wrinkle: we have to prepend an extra element at the front of the diff vector, because it has one fewer element than values did. Now we have a vector groups containing 1 for each element of values that should be in the first group, 2 for each element that should be in the second group, and so on.
You can access the individual groups using logical indexing:
values(groups == n) % returns a vector of the values in group n
If you want the groups as separate vectors you could use arrayfun:
groupArray = arrayfun(#(x) values(groups == x), 1:max(groups), 'UniformOutput', false)
This returns a cell array where groupArray{1} is the first group, groupArray{2} is the second group, etc.
Related
I have a matrix A of size 2500 x 500. I want to sum each 10 columns and get the result as a matrix B of size 2500 x 50. That is, the first column of B is the sum of the first 10 columns of A, the second column of B is the sum of second 10 columns of A, and so on.
How can I do that without a for loop? Since I have to do that hundreds of times and it is highly time consuming to do that using for loop.
First, we "block reshape" A, such that we have the desired number of columns. Therefore, we shamelessly steal the code from the great Divakar, and put in some minimal effort to generalize it. Then, we just need to sum along the second axis, and reshape to the original form.
Here's an example with five columns to be summed:
% Sample input data
A = reshape(1:100, 10, 10).'
[r, c] = size(A);
% Number of columns to be summed
n_cols = 5;
% Block reshape to n_cols, see https://stackoverflow.com/a/40508999/11089932
B = reshape(permute(reshape(A, r, n_cols, []), [1, 3, 2]), [], n_cols);
% Sum along second axis
B = sum(B, 2);
% Reshape to original form
B = reshape(B, r, c / n_cols)
That's the output:
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
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
B =
15 40
65 90
115 140
165 190
215 240
265 290
315 340
365 390
415 440
465 490
Hope that helps!
This can be done with splitapply. An advantage of this approach is that it works even if the group size does not divide the number of columns (the last group is smaller):
A = reshape(1:120, 12, 10).'; % example 10×12 data (borrowed from HansHirse)
n_cols = 5; % number of columns to sum over
result = splitapply(#(x)sum(x,2), A, ceil((1:size(A,2))/n_cols));
In this example,
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 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45 46 47 48
49 50 51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70 71 72
73 74 75 76 77 78 79 80 81 82 83 84
85 86 87 88 89 90 91 92 93 94 95 96
97 98 99 100 101 102 103 104 105 106 107 108
109 110 111 112 113 114 115 116 117 118 119 120
result =
15 40 23
75 100 47
135 160 71
195 220 95
255 280 119
315 340 143
375 400 167
435 460 191
495 520 215
555 580 239
I matrix
A = [123 123 123 99 99 99 32 32 32 40
40 40 22 22 34 34 34 152 152 152
92 92 92 91 91 91 146 146 146 119
3 3 96 96 96 136 136 136 68 68
]
B = [40 68 119]
How can I replace with zero value from A that have same value with B. required result:
C = [123 123 123 99 99 99 32 32 32 0
0 0 22 22 34 34 34 152 152 152
92 92 92 91 91 91 146 146 146 0
3 3 96 96 96 136 136 136 0 0
]
thank you...
Use ismember to obtain a logical mask of values of A that are in B, and then use that as a logical index to make those entries zero:
C = A; % define C equal to A
C(ismember(A,B)) = 0; % make elements from B equal to 0
Or, in a single line: multiply A by a mask that equals 1 for elements not in B and 0 for elements in B:
C = A.*~ismember(A,B); % multiply A by a mask to make elements from B equal to 0
I'd start down this road:
C = A;
for i = 1:numel(B)
C(C == B(i)) = 0;
end
The third line uses logical indexing - C == 20 is a logical matrix, true where the element is 20, false otherwise, and C(C == 20) = 0 sets the true elements to 0.
I have a matrix A
A=[f magic(10)]
A=
931142103 92 99 1 8 15 67 74 51 58 40
931142103 98 80 7 14 16 73 55 57 64 41
931142103 4 81 88 20 22 54 56 63 70 47
459200101 85 87 19 21 3 60 62 69 71 28
459200101 86 93 25 2 9 61 68 75 52 34
459200101 17 24 76 83 90 42 49 26 33 65
459200101 23 5 82 89 91 48 30 32 39 66
37833100 79 6 13 95 97 29 31 38 45 72
37833100 10 12 94 96 78 35 37 44 46 53
37833100 11 18 100 77 84 36 43 50 27 59
The first column are firm codes. The rest columns are firms' data, with each row referring to the firm in Column 1 in a given year. Notice that years may not be balance for every firms.
I would like to subtract sub-matrices according to the first column. For instance, for A(1:3,2:11) for 931142103:
A(1:3,2:11)
ans =
92 99 1 8 15 67 74 51 58 40
98 80 7 14 16 73 55 57 64 41
4 81 88 20 22 54 56 63 70 47
Same as 459200101 (which would be A(4:7,2:11)) and A(8:10,2:11) for 37833100.
I get a sense that the code should like this:
indices=find(A(:,1));
obs=size(A(:,1));
for i=1:obs,
if i==indices(i ??)
A{i}=A(??,2:11);
end
end
I have difficulties in indexing these complicated codes: 459200101 and 37833100 in order to gather them together. And how can I write the rows of my submatrix A{i}?
Thanks so much!
One approach with arrayfun -
%// Get unique entries from first column of A and keep the order
%// with 'stable' option i.e. don't sort
unqA1 = unique(A(:,1),'stable')
%// Use arrayfun to select each such submatrix and store as a cell
%// in a cell array, which is the final output
outA = arrayfun(#(n) A(A(:,1)==unqA1(n),:),1:numel(unqA1),'Uni',0)
Or this -
[~,~,row_idx] = unique(A(:,1),'stable')
outA = arrayfun(#(n) A(row_idx==n,:),1:max(row_idx),'Uni',0)
Finally, you can verify results with a call to celldisp(outA)
If values in column 1 always appear grouped (as in your example), you can use mat2cell as follows:
result = mat2cell(A, diff([0; find(diff(A(:,1))); size(A,1)]));
If they don't, just sort the rows of A according to column 1 before applying the above:
A = sortrows(A,1);
result = mat2cell(A, diff([0; find(diff(A(:,1))); size(A,1)]));
If you don't mind the results internally not being ordered, you can use accumarray for this:
[~,~,I] = unique(A(:,1),'stable');
partitions = accumarray(I, 1:size(A,1), [], #(I){A(I,2:end)});
I have matrix A of the size(4,192). It consists of 12 matrices of the size(4,4) aligned horizontally. I want to get matrix B with the size(12,16). B must get as follows:
suppose
A=[y1,y2,y3,...,y12]
in which yn is a 4*4 matrix. Then,
B=[y1,y4,y7,y10;
y2,y5,y8,y11;
y3,y6,y9,y12]
Is there an efficient/quicker (using no loop) way to do this using MATLAB?
You can try the following code:
ys1 = 2; % size(1) from submatrix (for the following example, use ys1 = 4 for the actual problem)
ys2 = 2; % size(2) from submatrix (for the following example, use ys2 = 4 for the actual problem)
ns1 = 3; % size(1) of final matrix in terms of submatrix (3 rows)
ns2 = 4; % size(2) of final matrix in terms of submatrix (4 columns)
temp = reshape(A,ys1,ys2,ns1,ns2);
B = reshape(permute(temp,[1 3 2 4]),ys1*ns1,ys2*ns2);
Example:
A = [11 12 21 22 31 32 41 42 51 52 61 62 71 72 81 82 91 92 101 102 111 112 121 122;
13 14 23 24 33 34 43 44 53 54 63 64 73 74 83 84 93 94 103 104 113 114 123 124];
B =
11 12 41 42 71 72 101 102
13 14 43 44 73 74 103 104
21 22 51 52 81 82 111 112
23 24 53 54 83 84 113 114
31 32 61 62 91 92 121 122
33 34 63 64 93 94 123 124
I have a data-set, in which I want to extract columns 1-3, 7-9, 13-15, all the way to the end of the matrix
As an example, I've used the standard magic function to create a matrix
A=magic(10)
A =
92 99 1 8 15 67 74 51 58 40
98 80 7 14 16 73 55 57 64 41
4 81 88 20 22 54 56 63 70 47
85 87 19 21 3 60 62 69 71 28
86 93 25 2 9 61 68 75 52 34
17 24 76 83 90 42 49 26 33 65
23 5 82 89 91 48 30 32 39 66
79 6 13 95 97 29 31 38 45 72
10 12 94 96 78 35 37 44 46 53
11 18 100 77 84 36 43 50 27 59
I know that I can extract single columns starting at 1, in intervals of 3 with the command:
Aex=a(:,1 : 3 : end)
Aex =
92 8 74 40
98 14 55 41
4 20 56 47
85 21 62 28
86 2 68 34
17 83 49 65
23 89 30 66
79 95 31 72
10 96 37 53
11 77 43 59
Say I want to extract groups of columns instead (e.g. column 1-3, 7-9 etc.).
Is there a way to do this without having to manually point out all the column numbers?
Thanks for your help!
Rasmus
Is this what you are looking for:
Aex = A(:,[1:3 7:9])
?
I am assuming that you would like the result all concatenated into another large matrix?
If that is the case, try this one on for size:
result = A(diag(0:2)*ones(3,floor((size(A,2) - 3)/6) + 1) + ...
ones(3,floor((size(A,2) - 3)/6) + 1)*diag(1:6:(size(A,2)-3)))
That could probably be shortened with some matrix math rules. You could also parameterize the values so that it can be modified to do more than what this problem expects, (and also might make more sense),
a = 3;
b = 6;
result = A(diag(0:a-1)*ones(a,floor((size(A,2) - a)/b) + 1) + ...
ones(a,floor((size(A,2) - a)/b) + 1)*diag(1:b:(size(A,2)-a)))
where a is the size of "group" (length([1 2 3]) = length([7 8 9]) = ... = 3), etc. and b is the column spacing ([1...7...13...] in your example)
If you would like them separated, I put them in cells here, but they can go to wherever you need:
a = 3;
b = 6;
results = {};
for Cols = 1:b:(size(A,2)-a)
results{end+1} = A(:, Cols:(Cols+2));
end
I didn't check the speed of either of these, but I think the first one may be faster. You may want to split it up into terms so it's more readable, I just did it to fit on a single line (which isn't always the best way of writing code).
The simple way to do this:
M = magic(10);
n = size(M,2)
idx = sort([1:3:n 2:3:n 3:3:n])
M(:,idx)
If however, the pattern of removal is simpler than the pattern of colums that you want to keep you could use this instead:
A = magic(10);
B = A;
B(:,4:3:end)=[];
B(:,4:3:end)=[]; %Yes 3x the same line of code.
B(:,4:3:end)=[];