Matlab - array of matrices - matlab

I have two matrices A1 and A2, for example A1 = [1 0; 1 1]; and A2 = [0 1; 1 1];
Now I don't want to have them called A1 and A2 since I will have An matrices.
So I wanted something like
A(1) = [1 0; 1 1];
A(2) = [0 1; 1 1];
..
A(n) = [...];
But Matlab does not allow me to do this.
I know one can use A(:,:,1) = [ ... ] but this is ugly and makes me type :,:, all the time... so I want to know if there is a different solution.
I tried A.1 but structs field names need to be strings.

Use cell array's:
A = cell(N, 1);
A{1} = [ 1 0; 1 1 ];
A{2} = [ 0 1; 1 1 ];

You can use an array of structs.
A(1).mat = [1 0; 1 1];
A(2).mat = [0 0; 1 1];
...
A(n)...
or a cell array
A{1} = [1 0; 1 1];
A{2} = [0 1; 1 1];
...
A{n}...

Related

How to used a for loop to determine the set of index?

I trying to make a program that uses the for loop to calculate the set of index D. But I have a problem because the length of index are not the same.
Example:
z = [0 0 0 0 0 0 1]
v(1,:) = [1 0 0 0 1 0 1]
v(2,:) = [0 1 0 0 1 1 1]
v(3,:) = [0 0 1 0 1 1 0]
v(4,:) = [0 0 0 1 0 1 1]
v(1,:) = find(v(1,:)~=z);
v(2,:) = find(v(2,:)~=z);
v(3,:) = find(v(3,:)~=z);
v(4,:) = find(v(4,:)~=z);
we obtain :
D(1,:) = [1 5];
D(2,:) = [2 5 6];
D(3,:) = [3 5 6 7];
D(4,:) = [4 6];
Code :
for aa = 1:4
D(aa,:) = [find(v(aa,:)~=z)];
end
not work because length(D(1,:))~=length(D(2,:))~=length(D(3,:))
How I can use a loop to determine set of index D?
Thank you for any help!
One solution can be using cell like the following:
for aa = 1:4
D{aa} = [find(v(aa,:)~=z)];
end
You can use the matrix D, but initialize it beforehand like:
D = ones(size(v)) + length(z)
Then fill it like:
for ii = 1:size(z,1)
D(ii,v(ii,:)~=z) = find(v(ii,:)~=z);
end
Notice, I added the length of v to the matrix of ones, such that you are sure that the predefined numbers in the matrix are larger than any index, hence the min() will not freak out.

Adding binary numbers into vector in for path

For example my vector is
a = [0 1]
I want to add number 0 into vector but in FOR path 3 times. I want to get this vector
a = [0 1 0 0 0]
You don't need a loop to do this. This can be accomplished using concatenation and the zeros function.
nzeros = 3;
a = [0 1];
a = cat(2, a, zeros(1, nzeros));
% or a = [a zeros(1, nzeros)];
Alternatively:
nzeros = 3;
a = [0 1];
a(end+nzeros) = 0;
If you are talking about a for-loop. This will do what you asked for
a = [0 1];
for i=1:3
a = [a,0];
end

How to choose between two values using 'or' in matlab?

I have the following code :
a = cell(4,1);
a{1} = [5 3 0 0];
a{2} = [0 3 5 0];
a{3} = [1 3 0 0];
a{4} = [0 3 2 0];
arrayind = 2;
b = a(cellfun(#(x)x(arrayind) == 1,a));
b{:}
How can I achieve this when an IF statement is used :
if r>2
b = a(cellfun(#(x)x(arrayind) == (1 | 2 | 3),a));
end
Basically saying, find 1, if not there then 2, if not there then 3...
ismember could perhaps be what you are looking for.
Replacing the equality operator with ismember as follows:
a = cell(4,1);
a{1} = [5 3 0 0];
a{2} = [0 3 5 0];
a{3} = [1 3 0 0];
a{4} = [0 3 2 0];
arrayind = 1;
b = a(cellfun(#(x) ismember(x(arrayind), [1 5]), a));
would yield b = a([1, 3])

Generate boolean matrix by predicate on row and column

I have the following vector:
y = [1; 3; 2; 3; 1];
All its values are between 1 and n (in this case, 3) and denote different options.
I want to create a matrix of size size(y, 1) x n whose rows correpond to y values:
1 0 0 % because y(1) = 1
0 0 1 % because y(2) = 3
0 1 0 % because y(3) = 2
0 0 1
1 0 0
One way to do this would be
Y = zeros(size(y, 1), num_labels);
for i = 1:m
Y(i, y(i)) = 1;
end
Is there a better way to do this, maybe in a single expression?
Basically, what I need is to generate a matrix with boolean predicate (i, j) => j == y(i).
You can try this if a is a column vector
a = [1; 3; 2; 3; 1];
bsxfun(#eq, a, [1:max(a)])
and this if it is a row vector
a = [1; 3; 2; 3; 1]';
bsxfun(#eq, a', [1:max(a)])
If you have access to Statistics Toolbox, the command dummyvar does exactly what you need.
>> y = [1; 3; 2; 3; 1];
>> dummyvar(y)
ans =
1 0 0
0 0 1
0 1 0
0 0 1
1 0 0
You can use sub2ind after initializing the matrix as follows:
y = [1; 3; 2; 3; 1];
m = length(y);
n = max(y);
Y = zeros(m, n);
Y(sub2ind(size(Y), 1:m, y')) = 1
Y =
1 0 0
0 0 1
0 1 0
0 0 1
1 0 0
The trick here is to know that the corresponding rows of y go from 1 to m one by one.
accumarray([(1:length(y)).' y], 1)
As suggested by Dmitri Bouianov on Coursera discussion forum, this also works:
Y = eye(num_labels)(y, :);
This solution uses elements of y to as indices to select rows from an identity matrix.
In Octave (at least as of 3.6.3, not sure when it was introduced), you can use broadcasting to do this extremely easily. It works like this:
Y = y==1:3
(if y is a row matrix, you need to transpose it first - if you want to have Y transposed instead, use y==(1:3)')

Converting a matrix in MATLAB

Let's say I've got a vector a = [1 2 4]. I want it converted into a vector which looks like this b = [1 2 0 4], i.e. each number is placed into a correct position and since 3 is not included into the vector a, it is replaced by 0 in vector b. This can be done the following way:
a = [1 2 4]
b = zeros(1, size(a, 2));
b(1, a) = a;
I can't figure out a way to do the same for a matrix. For example,
c = [1 4 2 0; 3 1 0 0; 4 0 0 0; 1 3 4 0];
I need to convert into a matrix that looks like this:
d = [1 2 0 4; 1 0 3 0; 0 0 0 4; 1 0 3 4];
Any tips? How can this be done? How can I do this without using loops?
Here's a vectorized solution:
a = [1 4 2 0; 3 1 0 0; 4 0 0 0; 1 3 4 0];
b = zeros(size(a,1),max(a(:)));
[rowIdx,~] = find(a);
vals = a(a>0);
b( sub2ind(size(b),rowIdx,vals) ) = vals;
Does this work? (Edited: fixed mistake.)
[m,n] = size(c)
d = zeros(m,n)
for i=1:m
d(i,c(i,c(i,:)>0)) = c(i,c(i,:)>0)
end