I have following data matrix in Matlab, I am trying to actually split this into multiple segments by passing a variable to a matlab function. But before splitting I would like to shuffle the matrix. The size of my matrix is 150X4
s.data
5.1000 3.5000 1.4000 0.2000
4.9000 3.0000 1.4000 0.2000
4.7000 3.2000 1.3000 0.2000
4.6000 3.1000 1.5000 0.2000
5.0000 3.6000 1.4000 0.2000
..
s =
data: [150x4 double]
labels: [150x1 double]
Coming from R environment I find MatLab is very strange. Initially I thought the columns in matrix has a relationshop like in a R dataframe but thats wrong in my assumption.
or you can do:
perm=randperm(numel(data)); % generate a random permutation
data = reshape(data(perm),size(data)); % apply it to data
new_data=data(randsample(1:length(data),length(data)),:)
Complementing the shuffle answers, in order to split your data into matrices of 15x2 each, you can use mat2cell:
data = rand(150,4); %# generates a random 150x4 matrix
rowdiv = repmat(15,1,10); %# size of each chunk in rows. Must sum to 150
coldiv = repmat(2,1,2); %# size of each chunk in cols. Must sum to 4
datacell = mat2cell(data, rowdiv, coldiv)
It will return a cell with 20 matrices, which are accessed by datacell{x,y}:
datacell =
[15x2 double] [15x2 double]
[15x2 double] [15x2 double]
[15x2 double] [15x2 double]
[15x2 double] [15x2 double]
[15x2 double] [15x2 double]
[15x2 double] [15x2 double]
[15x2 double] [15x2 double]
[15x2 double] [15x2 double]
[15x2 double] [15x2 double]
[15x2 double] [15x2 double]
B = repmat(A,M,N) creates a large matrix B consisting of an M-by-N tiling of copies of A. We are using it here to generate an exact division of the rows and columns, repeating element 15 ten times and 2 twice, respectively. But, you don't need to do an exact division. You can set chunks with different sizes. Row with different size:
rowdiv =
15 15 15 15 15 15 15 15 16 14
Will return:
datacell =
[15x4 double]
[15x4 double]
[15x4 double]
[15x4 double]
[15x4 double]
[15x4 double]
[15x4 double]
[15x4 double]
[16x4 double]
[14x4 double]
Related
I have A and B a cell array of matrices inside. I want to obtain C
A =
[18x18 double]
[18x18 double]
[18x18 double]
[18x18 double]
[18x18 double]
[18x18 double]
[18x18 double]
[18x18 double]
B =
[18x18 double]
[18x18 double]
[18x18 double]
[18x18 double]
[18x18 double]
[18x18 double]
[18x18 double]
[18x18 double]
K = magic(18);
In for loop:
C = cell(8,1);
for ii = 1:8
C{ii} = K*A{ii}'*B{ii};
end
How can I do this in a vectorized form (cell)?
Although it's possible,
K = repmat({K}, 8,1);
C = spblkdiag(K{:}) * spblkdiag(A{:}).' * spblkdiag(B{:});
C = reshape(nonzeros(C), 18,[]);
C = mat2cell(C, 18,18 * ones(8,1))';
I'd still suggest you use a loop.
Is it possible to obtain a matrix as follows??
The input vectors are X(column vector) and Y(row vector)
X=[2 Y=[5 3 1 2 4]-1*5 vector
4
5
3
1]-5*1 vector
both vectors have the index values as elements. Now I want to have a 5*5 matrix which is as follows:
Z= (2,5) (2,3) (2,1) (2,2) (2,4)
(4,5) (4,3) (4,1) (4,2) (4,4)
(5,5) (5,3) (5,1) (5,2) (5,4)
(3,5) (3,3) (3,1) (3,2) (3,4)
(1,5) (1,3) (1,1) (1,2) (1,4)
Z-5*5 matrix
is it possible to obtain a matrix like this using matlab...pls help....i have no idea how to do this....thanks in advance...
Here's an alternative solution using a cell array instead of a regular array:
XX=meshgrid(X);
YY=meshgrid(Y);
C=reshape(num2cell([XX(:) YY(:)],2),numel(X),[]);
The outcome will be a 5x5 cell array,
C =
[1x2 double] [1x2 double] [1x2 double] [1x2 double] [1x2 double]
[1x2 double] [1x2 double] [1x2 double] [1x2 double] [1x2 double]
[1x2 double] [1x2 double] [1x2 double] [1x2 double] [1x2 double]
[1x2 double] [1x2 double] [1x2 double] [1x2 double] [1x2 double]
[1x2 double] [1x2 double] [1x2 double] [1x2 double] [1x2 double]
each element will contain the 2 numbers. For example:
C{2,2}
ans =
4 3
Maybe this is what you want:
Z = cat(3, repmat(X, 1, size(Y,2)), repmat(Y, size(X,1), 1));
This builds a 3D-array Z such that Z(m,n,:) gives the m,n entry of your "matrix".
However, depending on what you want to achieve, there are probably better ways to do it.
Consider a cell array ,
H = [ {N1x1} {N2x1} {N3x1} ...{Nmx1} ]
How does get (efficiently) all pairwise intersections of these cells?
Not sure how efficient this will be.
N = numel(H);
[ii jj] = ndgrid(1:N);
result = arrayfun(#(n) intersect(H{ii(n)},H{jj(n)}), 1:N^2, 'uni', 0);
result = reshape(result,N,N);
Example:
H = {[1 2 3], [2 3], [4 5]};
gives
result =
[1x3 double] [1x2 double] [1x0 double]
[1x2 double] [1x2 double] [1x0 double]
[1x0 double] [1x0 double] [1x2 double]
>> result{1,1}
ans =
1 2 3
>> result{1,2}
ans =
2 3
>> result{1,3}
ans =
Empty matrix: 1-by-0
[..]
This also works if H is a multidimensional cell array.
You could also use two for loops. Then you could save half operations explotiing the symmetry of the result.
I am using this function I got from the internet:
>>[Y,U,V]=yuv_import('test.yuv',[176 144],150,0)
I got this from: Convert YUV CIF 4:2:0 video file to image files
It prints out the Y, U and V components of the yuv file test.yuv. When I typed:
>>Y
It displayed:
Y =
Columns 1 through 5
[144x176 double] [144x176 double] [144x176 double] [144x176 double] [144x176 double]
...............
Columns 146 through 150
[144x176 double] [144x176 double] [144x176 double] [144x176 double] [144x176 double]
And..
>>size(Y)
displayed:
ans =
1 150
Doing the same for U and V components also showed the same results.
And also..
>>Y(150)
displayed:
ans =
[144x176 double]
What I want is make an array for Y, U and V that has the dimensions [numberOfFrames height width] or [150 144 176]. How can I do this?
Your outputs are cell-arrays.
>> Y = cat(3, Y{:} );
should do the trick for you.
I manage to do a 10 fold and store the data into a cell and my cell has the following structure:
[135x5 double] [15x5 double]
[135x5 double] [15x5 double]
[135x5 double] [15x5 double]
[135x5 double] [15x5 double]
[135x5 double] [15x5 double]
[135x5 double] [15x5 double]
[135x5 double] [15x5 double]
[135x5 double] [15x5 double]
[135x5 double] [15x5 double]
[135x5 double] [15x5 double]
here is a small snapshot of what data is in this cell, let say we assign this cell in to a variable cell here is the cell{1,1} - This is actually Iris data
5.1000 3.3000 1.7000 0.5000 1.0000
6.8000 3.2000 5.9000 2.3000 3.0000
5.0000 2.3000 3.3000 1.0000 2.0000
7.4000 2.8000 6.1000 1.9000 3.0000
6.5000 3.2000 5.1000 2.0000 3.0000
4.8000 3.4000 1.9000 0.2000 1.0000
cell{1,2}
7.2000 3.2000 6.0000 1.8000 3.0000
6.1000 2.6000 5.6000 1.4000 3.0000
6.4000 2.9000 4.3000 1.3000 2.0000
6.8000 3.0000 5.5000 2.1000 3.0000
6.1000 2.8000 4.0000 1.3000 2.0000
Now I am trying to iterate over each of the row and anyalze the data in first column Cell{1,1}, Cell{1,2)... How can I do that? What is the technique to iterate over cell?
Does this cut-down example solve your problem?
Z = cell(2, 2);
Z{1, 1} = rand(8, 5); Z{1, 2} = rand(2, 5);
Z{2, 1} = rand(8, 5); Z{2, 2} = rand(2, 5);
X = cell2mat(Z(:, 1));
XFirstCol = X(:, 1);
I use cell2mat to concatenate all the matrices in the first column of your cell array into one big matrix, and then the last line just grabs the first column of that matrix.
If you were instead asking how to loop over a cell array, well, you do it the same way as over a numeric array, but using curly braces to index the elements of the cell array ie:
for i = 1:2
CurrentCell = Z{i, 1};
FirstColumnOfCurrentCell = CurrentCell(:, 1);
end
Or you could combine those two lines into: FirstColumnOfCurrentCell = Z{i, 1}(:, 1);
A final point, don't use cell as the name of a variable. This is not good practice, since cell is also the name of an in-built matlab function