Creating a 2D matrix in Matlab - arrays

I want to Create a matrix like
A = [1 2 3 ;4 5 6; 7 8 9];
I want to do it like this,
A = 1 + val : 1 : 3 + val ;
val = [0 3 6];
But I am only getting [1 2 3] , Not 2D matrix .

Try this,
val = [0 3 6];
A = bsxfun(#plus,val',1:3);
A =
1 2 3
4 5 6
7 8 9

Related

Is there a matlab function for splitting an array for several array?

I want to split an array into several arrays automatically. For example:
a=[1 2 3 4 5 6 7 8 9]
b=[2 5]
Thus, I want to split it to:
c1=[1 2]
c2=[3 4 5]
c3=[6 7 8 9]
How to do it?
A simple way is to use mat2cell:
a = [1 2 3 4 5 6 7 8 9];
b = [2 5];
c = mat2cell(a, 1, diff([0 b numel(a)]));
This gives a cell array c containing the subarrays of a:
>> celldisp(c)
c{1} =
1 2
c{2} =
3 4 5
c{3} =
6 7 8 9

How to Apply Cellfun Vectors with Arrayfun, Matlab?

I am expanding the arrayfun code of the thread To Find Double Sequence With Transforms in Matlab? for a list of vectors in cellfun (DD).
Pseudocode
DD = {[9 1 5 6 6 6 5 1 1 0 7 7 7 7 7 8], [1 1 1 4], [7 7 7]};
d = cellfun(#(i) diff(diff([0 i 0]) == 0), DD, 'Uniform', false);
y = cellfun(#(z) ...
arrayfun(#(i,j) DD{i}(i:j), find(z>0), find(z<0), 'Uniform', false), ...
d, 'Uniform', false););
Expected output
y = { {[6 6 6], [1 1], [7 7 7]}, ...
{[1 1 1]}, ...
{[7 7 7]} };
Error
Index exceeds matrix dimensions.
Error in findDoubleSequenceAnonFunction>#(i,j)DD{i}(i:j)
Error in
findDoubleSequenceAnonFunction>#(z)arrayfun(#(i,j)DD{i}(i:j),find(z>0),find(z<0),'Uniform',false)
Error in findDoubleSequenceAnonFunction (line 5)
y = cellfun(#(z) ...
Comments
d = cellfun(.... I am applying the function diff(diff(... in cellfun. It should be ok.
y = cellfun(.... Need to have cellfun here because have the again a cell of vectors in d. Somehow, the cellfun-arrayfun is complicating.
How can you have cellfun-arrayfun combination here?
Just use a for-loop, easier to read:
XX = {[9 1 5 6 6 6 5 1 1 0 7 7 7 7 7 8], [1 1 1 4], [7 7 7]};
YY = cell(size(XX));
for i=1:numel(XX)
x = XX{i};
d = diff(diff([0 x 0]) == 0);
YY{i} = arrayfun(#(i,j) x(i:j), find(d>0), find(d<0), 'Uniform',false);
end
Result:
>> celldisp(YY)
YY{1}{1} =
6 6 6
YY{1}{2} =
1 1
YY{1}{3} =
7 7 7 7 7
YY{2}{1} =
1 1 1
YY{3}{1} =
7 7 7

Building all possible arrays from vector of subarrays. With recursion [duplicate]

This question already has answers here:
Generate a matrix containing all combinations of elements taken from n vectors
(4 answers)
Closed 8 years ago.
I'm trying to build all possible arrays of length n of a vector of n elements with at least 2 integers in each position. I should be getting 2^n combinations, 16 in this case. My code is generating only half of them, and not saving the output to an array
allinputs = {[1 2] [2 3] [3 4] [5 6]}
A = []
the command I run is
inputArray = inputBuilder(A,[],allinputs,1)
for the function
function inputArray = inputBuilder(A,currBuild, allInputs, currIdx)
if currIdx <= length(allInputs)
for i = 1:length(allInputs{currIdx})
mybuild = [currBuild allInputs{currIdx}(i)];
inputBuilder(A,mybuild,allInputs,currIdx + 1);
end
if currIdx == length(allInputs)
A = [A mybuild];
%debug output
mybuild
end
if currIdx == 1
inputArray = A;
end
end
end
I want all 16 arrays to get output in a vector. Or some easy way to access them all. How can I do this?
EDIT:
Recursion may be a requirement because allinputs will have subarrays of different lengths.
allinputs = {[1] [2 3] [3 4] [5 6 7]}
with this array it will be 1*2*2*3 or 12 possible arrays built
Not sure exactly if this is what you want, but one way of doing what I think you want to do is as follows:
allinputs = {[1 2] [2 3] [3 4] [5 6]};
comb_results = combn([1 2],4);
A = zeros(size(comb_results));
for rowi = 1:size(comb_results, 1)
indices = comb_results(rowi,:);
for idxi = 1:numel(indices)
A(rowi, idxi) = allinputs{idxi}(indices(idxi));
end
end
This gives:
A =
1 2 3 5
1 2 3 6
1 2 4 5
1 2 4 6
1 3 3 5
1 3 3 6
1 3 4 5
1 3 4 6
2 2 3 5
2 2 3 6
2 2 4 5
2 2 4 6
2 3 3 5
2 3 3 6
2 3 4 5
2 3 4 6
combn is here.

how to replicate an array

I want to make a function like this
>> matdup([1 2],3,4) %or any other input that user wish to enter
ans=
1 2 1 2 1 2 1 2
1 2 1 2 1 2 1 2
1 2 1 2 1 2 1 2
I am stuck in my code. My logic:
m = matdup(input,row,col)
for i = 1:row
for j = 1:col
m(i, j)= input;
This is producing this:
>> matdup(1,2,2)
ans=
1 1
1 1
But failed at this:
>> matdup([1 2],3,4)
error at console:
Subscripted assignment dimension mismatch.
Error in ==> matdup at 6
m(i, j)= input
Any idea?
Method 1: Are you allowed to use ones? Try this -
A = [1 2]
rowIdx = [1 : size(A,1)]';
colIdx = [1 : size(A,2)]';
out = A(rowIdx(:, ones(3,1)), colIdx(:, ones(4,1)))
Output
out =
1 2 1 2 1 2 1 2
1 2 1 2 1 2 1 2
1 2 1 2 1 2 1 2
Method 2: Are you allowed to use bsxfun and permute? Try this for the same result -
A = [1 2]
row_mapped = bsxfun(#plus,A,zeros(3,1))
out = reshape(bsxfun(#plus,row_mapped,permute(zeros(4,1),[3 2 1])),[3 8])
Matlab has a funcion called repmat that does the same.
If you want to create a similar function, you could do something like this:
function B = matdup(A, M, N)
[nr, nc] = size(A);
B = zeros([nr nc] .* [M N]);
for r = 1:M
for c = 1:N
rr = (r - 1) * nr + 1;
cc = (c - 1) * nc + 1;
B(rr:rr + nr - 1, cc:cc + nc - 1) = A;
end
end
end
Note this function is restricted to 2D matrices.
Try kron:
matdup = #(x,m,n) kron(ones(m,n),x)
Demonstration:
>> A = [5 6 7];
>> out = matdup(A,3,2)
out =
5 6 7 5 6 7
5 6 7 5 6 7
5 6 7 5 6 7
Note that you can switch the inputs to kron to effectively replicate elements rather than the whole matrix:
repel = #(x,m,n) kron(x,ones(m,n));
Demonstration:
>> A = [5 6 7];
>> out = repel(A,3,2)
out =
5 5 6 6 7 7
5 5 6 6 7 7
5 5 6 6 7 7
The replication can be done easily using mod:
function R = matdup(A, M, N)
[m n]= size(A);
R = A(mod(0:m*M-1,m)+1, mod(0:n*N-1,n)+1)

How to store some grouped values in an array, vector or matrix structure in matlab?

My problem is about storing some changeble number of value groups as one dimensional in an array, a vector or a matrix in matlab.
If we think the values are like this:
1 1 1 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 5 5 5 6 6 6 6 6....
I want to keep them dynamically in a structure as each group is one element of this structure, for example:
a = [1 1 1 1 1 1 1 1]
b = [2 2 2 2 2]
c = [3 3 3 3 3 3 3]
d = [4 4 4 4 4 4 4]
e = [5 5 5]
f = [6 6 6 6 6]
x = [a,b,c,d,e,f]
How can I do this?
Use a structure:
>> myStruct.a = [1 1 1 1 1 1 1 1];
>> myStruct.b = [2 2 2 2 2];
>> myStruct.c = [3 3 3 3 3 3 3];
>> myStruct.d = [4 4 4 4 4 4 4];
>> myStruct.e = [5 5 5];
>> myStruct.f = [6 6 6 6 6]
myStruct =
a: [1 1 1 1 1 1 1 1]
b: [2 2 2 2 2]
c: [3 3 3 3 3 3 3]
d: [4 4 4 4 4 4 4]
e: [5 5 5]
f: [6 6 6 6 6]
Or, if you want to numerically index your object, use a cell array:
>> myCell{1} = [1 1 1 1 1 1 1 1];
>> myCell{2} = [2 2 2 2 2];
>> myCell{3} = [3 3 3 3 3 3 3];
>> myCell{4} = [4 4 4 4 4 4 4];
>> myCell{5} = [5 5 5];
>> myCell{6} = [6 6 6 6 6];
>> myCell{:}
ans =
1 1 1 1 1 1 1 1
ans =
2 2 2 2 2
ans =
3 3 3 3 3 3 3
ans =
4 4 4 4 4 4 4
ans =
5 5 5
ans =
6 6 6 6 6

Resources