Let A and B be column vectors:
A = (1:6).'; %'// [1;2;3;4;5;6]
B = (7:12).'; %'// [7;8;9;10;11;12]
How do I construct a matrix such that one element of A is replaced each time by an element from B to get C as:
C = [...
7 1 1 1 1 1
2 8 2 2 2 2
3 3 9 3 3 3
4 4 4 10 4 4
5 5 5 5 11 5
6 6 6 6 6 12];
If C is to be indexed as index = [1 1 1 2 2 3 3]', how is it possible to produce
c1 = [...
1 7 1 1
1 2 8 2
1 3 3 9];
c2 = [...
2 10 4
2 5 11];
c3 = [3 6]; %// either this
c3 = [3 12]; %// or this
Problem #1:
C = bsxfun(#plus,A.',-diag(A)+diag(B));
gives:
7 2 3 4 5 6
1 8 3 4 5 6
1 2 9 4 5 6
1 2 3 10 5 6
1 2 3 4 11 6
1 2 3 4 5 12
Problem #2:
As far as I understand, you want to extract blocks out of the previously found C matrix, in the following fashion:
Where c1, c2, c3 are the green, red and blue blocks, repectively; and "missing" elements are replaced with NaN (in the initial version of your question).
index = [1 1 1 2 2 3 3]; %// Block definition
[blkSz,~] = histcounts(index,numel(unique(index))); %// Conversion...
paddedC = padarray(C,numel(index)-numel(diag(C))*[1 1],NaN,'post');
blocks = mat2cell(paddedC,blkSz,blkSz);
This results in a cell-array blocks with the following contents:
blocks =
[3x3 double] [3x2 double] [3x2 double]
[2x3 double] [2x2 double] [2x2 double]
[2x3 double] [2x2 double] [2x2 double]
Where for example blocks{1,1} is:
ans =
7 2 3
1 8 3
1 2 9
Then you can pad the array using the index of the loop, to get the c cell, like so:
c = cell(numel(blkSz),1); %// Preallocation
for ind1 = 1:numel(blkSz)
c{ind1} = padarray(blocks{ind1,ind1},1,ind1,'pre');
end
Note that c1 is found in c{1} etc.
Addition:
In the case when you don't want c1...c3 to be (possibly) padded with NaNs, the best way in my opinion would be trimming the index vector to the length of A or B ("length(C)"). This ensures you won't access blocks outside your square matrix. For example:
newIdx = index(1:numel(A));
Then just use newIdx instead of index in the rest of the code.
Related
I have a matrix as follows:
A= 1 2
3 4
5 6
7 8
I want to arrange the elements of this matrix in such a way that it will give me the following output:
B= 1
2
3
4
5
6
7
8
Any kind of suggestion will be helpful. Thanks!
Take the transpose of A and unroll it into a vector:
B = A.';
B = B(:);
Alternatively, you can use reshape:
B = reshape(A.', [], 1);
The reason why you transpose the matrix A first is because MATLAB does the unrolling in column-major format, which means that the columns are traversed first. You are trying to do this row-wise, and so you'd need to transpose the input to achieve the same effect.
Here's what the output looks like (using both):
>> A= [1 2
3 4
5 6
7 8];
>> B = A.';
>> B = B(:);
>> B
B =
1
2
3
4
5
6
7
8
Also:
>> A= [1 2
3 4
5 6
7 8];
>> B = reshape(A.', [], 1)
B =
1
2
3
4
5
6
7
8
This question already has answers here:
Got confused with a vector indexed by a matrix, in Matlab
(2 answers)
Closed 8 years ago.
Suppose:
a =
1 2 3
4 5 6
2 3 4
and
b =
1 3 2
6 4 8
In MATLABa(b) gives:
>> a(b)
ans =
1 2 4
3 2 6
What is the reason for this output?
when you have a matrix a:
a =
1 2 3
4 5 6
7 8 9
and b:
b =
1 3 4
3 2 6
then a(b) is a way of adressing items in a and gives you:
>> a(b)
ans =
1 7 2
7 4 8
to understand this you have to think of a als a single column vector
>> a(:)
ans =
1
4
7
2
5
8
3
6
9
now the first row of b (1 3 4) addresses elements in this vector so the first, the 3rd and the forth element of that single column vector which are 1 7 and 2 are adressed. Next the secound row of b is used as adresses for a secound line in the output so the 3rd, the 2nd and the 6th elements are taken from a, those are 7 4 and 8.
It's just a kind of matrix indexing.
Matrix indexes numeration in 'a' matrix is:
1 4 7
2 5 8
3 6 9
This is a possible duplicate to this post where I gave an answer: Got confused with a vector indexed by a matrix, in Matlab
However, I would like to duplicate my answer here as I think it is informative.
That's a very standard MATLAB operation that you're doing. When you have a vector or a matrix, you can provide another vector or matrix in order to access specific values. Accessing values in MATLAB is not just limited to single indices (i.e. A(1), A(2) and so on).
For example, let's say we had a vector a = [1 2 3 4]. Let's also say we had b as a matrix such that it was b = [1 2 3; 1 2 3; 1 2 3]. By doing a(b) to access the vector, what you are essentially doing is a lookup. The output is basically the same size as b, and you are creating a matrix where there are 3 rows, and each element accesses the first, second and third element. Not only can you do this for a vector, but you can do this for a matrix as well.
Bear in mind that when you're doing this for a matrix, you access the elements in column major format. For example, supposing we had this matrix:
A = [1 2
3 4
5 6
7 8]
A(1) would be 1, A(2) would be 3, A(3) would be 5 and so on. You would start with the first column, and increasing indices will traverse down the first column. Once you hit the 5th index, it skips over to the next column. So A(5) would be 2, A(6) would be 4 and so on.
Here are some examples to further your understanding. Let's define a matrix A such that:
A = [5 1 3
7 8 0
4 6 2]
Here is some MATLAB code to strengthen your understanding for this kind of indexing:
A = [5 1 3; 7 8 0; 4 6 2]; % 3 x 3 matrix
B = [1 2 3 4];
C = A(B); % C should give [5 7 4 1]
D = [5 6 7; 1 2 3; 4 5 6];
E = A(D); % E should give [8 6 3; 5 7 4; 1 8 6]
F = [9 8; 7 6; 1 2];
G = A(F); % G should give [2 0; 3 6; 5 7]
As such, the output when you access elements this way is whatever the size of the vector or matrix that you specify as the argument.
In order to be complete, let's do this for a vector:
V = [-1 9 7 3 0 5]; % A 6 x 1 vector
B = [1 2 3 4];
C = V(B); % C should give [-1 9 7 3]
D = [1 3 5 2];
E = V(D); % E should give [-1 7 0 9]
F = [1 2; 4 5; 6 3];
G = V(F); % G should give [-1 9; 3 0; 5 7]
NB: You have to make sure that you are not providing indexes that would make the accessing out of bounds. For example if you tried to specify the index of 5 in your example, it would give you an error. Also, if you tried anything bigger than 9 in my example, it would also give you an error. There are 9 elements in that 3 x 3 matrix, so specifying a column major index of anything bigger than 9 will give you an out of bounds error.
Suppose there are 2 vectors (i,j), A(10,1), B(10,1) which have obtain random values from [1,10] interval. eg.
A = [1 6 1 10 1 7 1 9 3 6]
B = [7 2 3 5 6 8 7 9 10 2].
I am interested in creating a new vector which will count how many values with the same i index occur. e.g.
1 and 7 ⇒ 2 occurrences
6 and 2 ⇒ 2 occurrences
1 and 3 ⇒ 1 occurrence
10 and 5 ⇒ 1 occurrence
1 and 6 ⇒ 1 occurrence
...
etc.
So that a final array/vector C occurs, with all the possible pairs and their counted occurrence with size C(number_of_pairs,2).
Use accumarray and then find:
A = [1 6 1 10 1 7 1 9 3 6];
B = [7 2 3 5 6 8 7 9 10 2]; %// data
aa = accumarray([A(:) B(:)], 1); %// how many times each pair occurs
[ii jj vv] = find(aa);
C = [ii jj vv]; %// only pairs which occurr at least once
In your example, this gives
C =
6 2 2
1 3 1
10 5 1
1 6 1
1 7 2
7 8 1
9 9 1
3 10 1
Or perhaps aa or vv are what you need; I'm not sure about what your desired output is.
Another possible approach, inspired by #Rody's answer:
mat = [A(:) B(:)];
[bb ii jj] = unique(mat, 'rows');
C = [mat(ii,:) accumarray(jj,1)];
Something like this?
A = [1 6 1 10 1 7 1 9 3 6];
B = [7 2 3 5 6 8 7 9 10 2];
%// Find the unique pairs
AB = [A;B].';
ABu = unique(AB, 'rows');
%// Count the number of occurrences of each unique pair
%// (pretty sure there's a better way to do this...)
C = arrayfun(#(ii) ...
[ABu(ii,:) sum(all(bsxfun(#eq, AB, ABu(ii,:)),2))], 1:size(ABu,1), ...
'UniformOutput', false);
C = cat(1,C{:});
Result:
C =
1 3 1
1 6 1
1 7 2
3 10 1
6 2 2
7 8 1
9 9 1
10 5 1
Something like this?
C = zeros(10);
for k = 1:10
C(A(k), B(k)) = C(A(k), B(k)) + 1
end
I need to combine some elements into one element of a row matrix in MATLAB. For example, my matrix is
[1 2 3 4 5 6 7 8]. I want to transform the matrix like [1234 5678]. Please help me do this.
Consider the following anonymous function for a generalized numeric solution for combining single digits:
combineDigits = #(x) x*(10.^(length(x)-1:-1:0)).';
Each element of x must be on [0,9] (i.e. single digits). For your example input:
>> x = [1 2 3 4 5 6 7 8]
x =
1 2 3 4 5 6 7 8
>> x1 = x(1:4); x2 = x(5:end);
>> combineDigits(x1)
ans =
1234
>> combineDigits(x2)
ans =
5678
>>
The anonymous function combinedDigits can be applied with any general utility function, such as cellfun, to process blocks of data. For example, consider the 2x8 matrix M of digits, partitioned into a 2x2 cell array of 1x4 digit arrays:
>> M = randi(9,2,8)
M =
4 8 6 8 7 7 6 7
9 9 1 9 7 4 2 1
>> Mpartitioned = mat2cell(M,[1 1],[4 4]) % specify ANY valid partitioning here
Mpartitioned =
[1x4 double] [1x4 double]
[1x4 double] [1x4 double]
>> Mcompacted = cellfun(combineDigits,Mpartitioned)
ans =
4868 7767
9919 7421
You can do it as following:
a=[1 2 3 4 5 6 7 8];
vec1=a(1:4) %put a suitable number here
vec1=num2str(vec1);
vec1(ismember(vec1,' ')) = [];
vec1=str2double(vec1);
Similarly, perform the above sequence of operation on the latter half of the vector and simply concatenate them in the final vector.
Note: This solution works only for a row vector as you mentioned in the question (you mentioned row matrix, but I assume this is what you meant).
It depends. If you can guarantee that your row will always be a multiple of 4, you could do the following:
reshape(sum(bsxfun(#times,reshape(Q',4,[])',[1000,100,10,1]),2),[],size(Q,1))';
What this does is reshape the matrix like so:
[1 2 3 4 5 6 7 8 -> [1 2 3 4
8 7 6 5 4 3 2 1] 5 6 7 8
8 7 6 5
4 3 2 1]
We then multiple each row by [1000,100,10,1] and sum each row:
[1 2 3 4 [1000 200 30 4 [1234
5 6 7 8 -> 5000 600 70 8 -> 5678
8 7 6 5 8000 700 60 5 8765
4 3 2 1] 4000 300 20 1] 4321]
Finally we reshape the matrix again to get back to the original shape.
[1234 [1234 5678
5678 -> 8765 4321]
8765
4321]
First define your values in a numeric array:
a = [1 2 3 4 5 6 7 8];
Next convert to a string:
b = num2str(a);
Now remove all the spaces:
b(find(b == ' ')) = '';
Insert an empty space in the middle and then convert back to a numeric array (I think this is what you want(?)):
c = str2num([b(1:4),' ',b(5:end)]);
So that
>> c(1)
ans =
1234
>> c(2)
ans =
5678
Edit: Added 'end' to cover an arbitrary range.
I need some help in converting a 2X2 matrix to a 4X4 matrix in the following manner:
A = [2 6;
8 4]
should become:
B = [2 2 6 6;
2 2 6 6;
8 8 4 4;
8 8 4 4]
How would I do this?
In newer versions of MATLAB (R2015a and later) the easiest way to do this is using the repelem function:
B = repelem(A, 2, 2);
For older versions, a short alternative to the other (largely) indexing-based solutions is to use the functions kron and ones:
>> A = [2 6; 8 4];
>> B = kron(A, ones(2))
B =
2 2 6 6
2 2 6 6
8 8 4 4
8 8 4 4
Can be done even easier than Jason's solution:
B = A([1 1 2 2], :); % replicate the rows
B = B(:, [1 1 2 2]); % replicate the columns
Here's one more solution:
A = [2 6; 8 4];
B = A( ceil( 0.5:0.5:end ), ceil( 0.5:0.5:end ) );
which uses indexing to do everything and doesn't rely on the size or shape of A.
This works:
A = [2 6; 8 4];
[X,Y] = meshgrid(1:2);
[XI,YI] = meshgrid(0.5:0.5:2);
B = interp2(X,Y,A,XI,YI,'nearest');
This is just two-dimensional nearest-neighbor interpolation of A(x,y) from x,y ∈ {1,2} to x,y ∈ {0.5, 1, 1.5, 2}.
Edit: Springboarding off of Jason S and Martijn's solutions, I think this is probably the shortest and clearest solution:
A = [2 6; 8 4];
B = A([1 1 2 2], [1 1 2 2]);
A = [2 6; 8 4];
% arbitrary 2x2 input matrix
B = repmat(A,2,2);
% replicates rows & columns but not in the way you want
B = B([1 3 2 4], :);
% swaps rows 2 and 3
B = B(:, [1 3 2 4]);
% swaps columns 2 and 3, and you're done!
Here's a method based on simple indexing that works for an arbitrary matrix. We want each element to be expanded to an MxN submatrix:
A(repmat(1:end,[M 1]),repmat(1:end,[N 1]))
Example:
>> A=reshape(1:6,[2,3])
A =
1 3 5
2 4 6
>> A(repmat(1:end,[3 1]),repmat(1:end,[4 1]))
ans =
1 1 1 1 3 3 3 3 5 5 5 5
1 1 1 1 3 3 3 3 5 5 5 5
1 1 1 1 3 3 3 3 5 5 5 5
2 2 2 2 4 4 4 4 6 6 6 6
2 2 2 2 4 4 4 4 6 6 6 6
2 2 2 2 4 4 4 4 6 6 6 6
To see how the method works, let's take a closer look at the indexing. We start with a simple row vector of consecutive numbers
>> m=3; 1:m
ans =
1 2 3
Next, we extend it to a matrix, by repeating it M times in the first dimension
>> M=4; I=repmat(1:m,[M 1])
I =
1 2 3
1 2 3
1 2 3
1 2 3
If we use a matrix to index an array, then the matrix elements are used consecutively in the standard Matlab order:
>> I(:)
ans =
1
1
1
1
2
2
2
2
3
3
3
3
Finally, when indexing an array, the 'end' keyword evaluates to the size of the array in the corresponding dimension. As a result, in the example the following are equivalent:
>> A(repmat(1:end,[3 1]),repmat(1:end,[4 1]))
>> A(repmat(1:2,[3 1]),repmat(1:3,[4 1]))
>> A(repmat([1 2],[3 1]),repmat([1 2 3],[4 1]))
>> A([1 2;1 2;1 2],[1 2 3;1 2 3;1 2 3;1 2 3])
>> A([1 1 1 2 2 2],[1 1 1 1 2 2 2 2 3 3 3 3])
There is a Reshape() function that allows you to do this...
For example:
reshape(array, [64, 16])
And you can find a great video tutorial here
Cheers