How can I extract the elements: [1,2,5,6], [3,4,7,8], [9,10,13,14], [11,12,15,16] ?
A = [1, 2, 3, 4;
5, 6, 7, 8;
9, 10, 11, 12;
13, 14, 15, 16;];
I'm using octave.
Best regards, Chris.
If you need four matrices then use
out = mat2cell(A,[2 2], [2 2]);
If you need four vectors with values , then use
out = cellfun(#(x)(reshape(x,1,[])),mat2cell(A,[2 2], [2 2]),'UniformOutput',0);
output will be
out{:,:}
ans =
1 5 2 6
ans =
9 13 10 14
ans =
3 7 4 8
ans =
11 15 12 16
Thanks, to Joe Serrano ,If you need the value in each of the four vectors in same order use,
out = cellfun(#(x)(reshape(x',1,[])),mat2cell(A,[2 2], [2 2]),'UniformOutput',0);
output will be
out{:,:}
ans =
1 2 5 6
ans =
9 10 13 14
ans =
3 4 7 8
ans =
11 12 15 16
Related
Suppose I have the following data:
xData = [4 7 2 1 2 8 7 1 1 3];
yData = [1 2 3 4 5 6 7 8 9 10];
P = [5 10 4 2 7 3 8 1 9 3];
I want to use a different markertype based on P. If the corresponding element in P<5 then 'o' and if P>5 then '^'. I know how to do this based on colour (although I don't actually know how to specify what colours to use?) but can this be done with markertype?
scatter(xData,yData,70,P>5)
Any ideas? Thanks!
You will need to do 2 scatter plots with less and more:
xData = [4 7 2 1 2 8 7 1 1 3];
yData = [1 2 3 4 5 6 7 8 9 10];
P = [5 10 4 2 7 3 8 1 9 3];
x_less = xData(P < 5);
x_more = xData(P >= 5);
y_less = yData(P < 5);
y_more = yData(P >= 5);
figure;
scatter(x_less, y_less, 20, 'r', 'o')
hold on
scatter(x_more, y_more, 20, 'b', '^')
This will give you an example like this:
Hope this helps.
For example, I have a 4x4 matrix, I want to split the max into 2x2 sub_regions. and do max on the sub_region.
1, 2, 3, 4
5, 6, 7, 8
9, 10, 11, 12
13, 14, 15, 16
\|/
6, 6, 8, 8
6, 6, 8, 8
14, 14, 16, 16
14, 14, 16, 16
Does this suit your need?
(Assuming your data is in a matrix called M)
>> cellfun(#(x) max(x(:)), mat2cell(M, [2 2], [2 2]))
ans =
6 8
14 16
EDIT:
You could also include kron to achieve your desired output:
>> kron(cellfun(#(x) max(x(:)), mat2cell(M, [2 2], [2 2])), ones(2))
ans =
6 6 8 8
6 6 8 8
14 14 16 16
14 14 16 16
colfilt will get the job done:
>> M = 2; N = 2;
>> B = colfilt(A,[M N],'distinct',#(x)repmat(max(x),[M*N 1]))
B =
6 6 8 8
6 6 8 8
14 14 16 16
14 14 16 16
The key is to use the 'distinct' block type option. Test data: A = reshape(1:16,4,4).'.
You can also use blockproc if you prefer:
B = blockproc(A,[M N],#(b) repmat(max(b.data(:)),[M N]))
OR
B = kron(blockproc(A,[M N],#(b) max(b.data(:))),ones(M,N))
Note: Image Processing Toolbox required for both.
I am parsing multiple text files in MATLAB and each time I store the result in the main array, but the problem that the data haven't the same size!
example:
t(i,:) = x;
% x data array from file i
ex:
t(1,:) = [ 5 4 3 2 1];
t(2,:) = [ 10 9 8 7 6 5];
t(3,:) = [ 11 12 13 14];
the size of x's is different, how i can store such these data (dynamic size) in the array!
Thanks,
you can store it in a cell array like this:
t{1} = [ 5 4 3 2 1];
t{2} = [ 10 9 8 7 6 5];
t{3} = [ 11 12 13 14];
and use them like this:
>> t(1)
ans =
[1x5 double]
>> t{2}
ans =
10 9 8 7 6 5
>> t
t =
[1x5 double] [1x6 double] [1x4 double]
>> t{:}
ans =
5 4 3 2 1
ans =
10 9 8 7 6 5
ans =
11 12 13 14
>> t{2}(1,2)
ans =
9
>> t{2}(1,2:end)
ans =
9 8 7 6 5
You can use a cell array to hold numeric arrays of various sizes. For instance
rows_cell = {};
rows_cell{1} = [ 5 4 3 2 1];
rows_cell{2} = [ 10 9 8 7 6 5];
rows_cell{3} = [ 11 12 13 14];
To access data:
rows_cell{2}(1,2)
ans =
9
rows_cell{3}(1,4)
ans =
14
I have the following matrix:
>> MatrixA = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]
MatrixA =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
I want to find the following:
for row 1, I want the 2nd column
for row 2, I want the 3rd column
for row 3, I want the 4th column
for row 4, I want the 4th column
Currently I accomplish this with the following line:
>>diag(MatrixA([1 2 3 4], [2 3 4 4]))
ans =
2
7
12
16
Is there a more direct way to do this (without using diag)?
Well you could use sub2ind, it might be more intuitive. I don't think there is much benefit though, maybe it's more readable:
ind = sub2ind(size(MatrixA), [1 2 3 4], [2 3 4 4])
MatrixA(ind)
What is the fastest and the simplest way to generate an array like
[0, 1, 3, 4, 6, 7, 9, 10, ...]
in MATLAB?
You can obtain the cumulative sum of the vector of steps (in your case it is [1 2 1 2 1 2 1 2 ...]). For example:
x = cumsum([0, repmat([1 2], 1, 4)])
x =
0 1 3 4 6 7 9 10 12
You can generate matrix with two rows: top row for odd array elements, bottom row for even elements. Than transform matrix into array with reshape.
>> a=[0:3:15; 1:3:16]
a =
0 3 6 9 12 15
1 4 7 10 13 16
>> a=reshape(a,1,12)
a =
0 1 3 4 6 7 9 10 12 13 15 16
Not one line but will work for either an odd or even number of total elements, and could be expanded if you wanted more than two different steps:
a = zeros(1,8);
a(1:2:end) = 0:3:10;
a(2:2:end) = 1:3:10;
Here is a simple and compact way:
A = 0:20;
A(3:3:end) = []