How to choose between two values using 'or' in matlab? - 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])

Related

Vectorization instead of for loop in Octave [duplicate]

Is there a matlab function which allows me to do the following operation?
x = [1 2 2 3];
and then based on x I want to build the matrix m = [1 2 2 3; 1 2 2 3; 1 2 2 3; 1 2 2 3]
You are looking for the REPMAT function:
x = [1 2 2 3];
m = repmat(x,4,1);
You can also use indexing to repeat the rows:
m = x(ones(4,1),:);
or even outer-product:
m = ones(4,1)*x;
and also using BSXFUN:
m = bsxfun(#times, x, ones(4,1))
You could try using vertcat, like this:
x = [1 2 2 3];
m = vertcat(x,x,x,x);
Or even simply:
x = [1 2 2 3];
m = [x;x;x;x];
EDIT:
for multiples of x, you can do:
x = [1 2 2 3];
m = [x;2*x;3*x]; % [1 2 2 3; 2 4 4 6; 3 6 6 9]
EDIT2:
For an arbitrary number of x's in m...
n = 3; % number of repetitions...
x = [1 2 2 3];
m = [];
for i=1:n
m = [m;x];
end

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.

Matlab: Add vectors not in the same length to a matrix

Is it possible to automatically add vectors that are not in the same length together for a matrix?
i.e:
a = [1 2 3 4]
b = [1 2]
How can I make C to be:
c = [1 2 3 4 ; 1 2 0 0]
or
c = [1 2 3 4 ; 1 2 NaN NaN]
or something like that
Thanks
This might help
a = [1 2 3 4];
b = [1 2];
c = a;
c(2,1:length(b)) = b;
c =
1 2 3 4
1 2 0 0
then, if you'd rather have NaN than 0, you could do what Dennis Jaheruddin suggests in a comment below.
Make a function like this
function out = cat2(a, b)
diff = length(a) - length(b)
if diff > 0
b = [b, nan(1, diff)];
else
a = [a, nan(1, -diff)];
end
out = [a;b];
end
(but also add a check to handle column vectors too)
cat2([1 2 3 4], [1 2])
ans =
1 2 3 4
1 2 NaN NaN

Matlab - array of matrices

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}...

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