Combine columns of a matrix to vectors - matlab

How can I combine columns of a matrix to vectors?
I have:
1 2 3
4 5 6
7 8 9
and I want to have a struct with the field 'positions' that contains a vector in each row like this:
[1 2 3]
[4 5 6]
[7 8 9]

is this what you need?
A=reshape(1:9,[3 3])';
st=cell2struct(mat2cell(A,ones([1 size(A,1)]),size(A,2)),'positions',size(A,2))
with that you get struct array.
>> st(1).positions
ans =
1 2 3

Related

How to randomly permute some matrix into a bigger matrix in Matlab?

This is the main matrix:
a =
1 2 3 3 4 5 2 5 7
3 4 5 5 6 8 6 4 9
This main matrix contain 3 small matrix. The first one is:
[1 2 3;3 4 5]
The second one is:
[3 4 5;5 6 8]
The third one is:
[2 5 7;6 4 9]
I want to randomly permute these 3 matrix into the main matrix like this:
a =
2 5 7 3 4 5 1 2 3
6 4 9 5 6 8 3 4 5
How I can do that?
If you connect three 2D matrices, it is better to use a 3D matrix:
%get a 3d matrix
b=reshape(a,size(a,1),3,[]);
%randomly permute third dimension
c=b(:,:,randperm(size(b,3)));
%return to 2d representation
d=reshape(c,size(a));

In matlab, how to generate 1:n in a matrix form without using loop ? e.g. when n=6, I want [1 2;3 4;5 6] instead of [1 2 3 4 5 6]

In MATLAB, how to generate numbers 1:n in a 2x(n/2) matrix form without using loop ? e.g.when n=6, I want [1 2;3 4;5 6] instead of [1 2 3 4 5 6].
You need to use reshape function:
n = 10;
reshape(1:n,2,[])'
ans =
1 2
3 4
5 6
7 8
9 10

break a matrix to sub-matrices with equal 2nd column without using for loop

I have a matrix, L, with two columns. I want to find its sub-matrices have equal values on their 2nd column. I want to do that using MATLAB without any for loop.
example:
L=[1 2;3 2;4 6;5 3;7 3;1 3;2 7;9 7]
then the sub-matrices are:
[1 2;3 2] , [4 6] , [5 3;7 3;1 3] and [2 7;9 7]
You can use a combination of arrayfun + unique to get that -
[~,~,labels] = unique(L(:,2),'stable')
idx = arrayfun(#(x) L(labels==x,:),1:max(labels),'Uniform',0)
Display output -
>> celldisp(idx)
idx{1} =
1 2
3 2
idx{2} =
4 6
idx{3} =
5 3
7 3
1 3
idx{4} =
2 7
9 7
You can use accumarray directly or with a sorted array, depending on you want the order of the rows to be stable, or the order of the submatricxes to be stable.
Say you want the rows to be stable:
>> [L2s,inds] = sort(L(:,2));
>> M = accumarray(L2s,inds,[],#(v){L(v,:)});
>> M(cellfun(#isempty,M)) = []; % remove empty cells
>> celldisp(M)
M{1} =
1 2
3 2
M{2} =
5 3
7 3
1 3
M{3} =
4 6
M{4} =
2 7
9 7

Adding value to a cell element specified by array

I am looking for a vectorized solution!
I have two arrays for example:
idx=[1 1 1 2 2 3 3 4 4 4 5 5 6 7 8 8 8 9 9]; %%//Integers,sorted
val=[1 4 8 2 5 3 9 1 4 8 2 5 6 7 1 4 8 3 9]; %%//respective Values (could be anything)
Now i want to create a cell array which contains in his elements specified by idx the according values of val. So the result should be a [9x1] cell with:
[1 4 8]
[2 5]
[3 9]
[1 4 8]
[2 5]
[6]
[7]
[1 4 8]
[3 9]
I know I could loop over the values from 1 to 9 and use horzcat while idx is equal to my loop index but I am looking for a vectorized solution. Reason is, that I am trying to change a loop solution of a problem to vectorized solution yet I am stuck here
Use accumarray:
out = accumarray(idx(:),val(:),[],#(x){x},{});
mat2cell(val,1,diff([0,find(diff(idx)),numel(idx)]))
Maybe someone finds a possibility to get rid of the find, then it's probably faster.

Finding indices of multiple items in a matrix?

I have an array of elements and I want to see the indices of them inside another matrix
For example, for
A = [1 2 3]
B = [1 2 3 4 5 3 4 5 1 2 3]
then result array
C = [1 2 3 6 9 10 11]
that give the indices of 1 2 3
Is there any function or a short way to handle?
I think you want:
find(ismember(B, A))
ans =
1 2 3 6 9 10 11