Minus each value of data - matlab

I have sort of data,
A = [2 4 6 8 10]
B = [1 2 3 4 5 6 7 8 9 10]
How to write program that can subtracts each value of A from all values of B.
To better understand,
Take A = 2, subtract from all B = [1 2 3 4 5 6 7 8 9 10],
then take A = 4, subtract from all B = [1 2 3 4 5 6 7 8 9 10]
and so on...

If you want to create a new array C that contains, in row i the result of B-A(i), you use bsxfun:
A = [2 4 6 8 10];
B = [1 2 3 4 5 6 7 8 9 10];
C = bsxfun(#minus,B,A') %'#
C =
-1 0 1 2 3 4 5 6 7 8
-3 -2 -1 0 1 2 3 4 5 6
-5 -4 -3 -2 -1 0 1 2 3 4
-7 -6 -5 -4 -3 -2 -1 0 1 2
-9 -8 -7 -6 -5 -4 -3 -2 -1 0
If you want to create a new array C that contains the result of B-A(1)-A(2)-..., you write
C = B-sum(A)
C =
-29 -28 -27 -26 -25 -24 -23 -22 -21 -20

Related

repmat, the size of matrix or number of use

I have a matrix for which I extract each column and do repmat function for each of them to build another matrix. Since i I have to do this for a large number of vectors(each column of my first matrix) it takes so long(relative to which I expect). If I do this for the whole matrix and then do something to build them, does it takes less time?
Consider this as an example:
A=[1 4 7;2 5 8;3 6 9]
I want to produce these
A1=[1 2 3 1 2 3 1 2 3
1 2 3 1 2 3 1 2 3
1 2 3 1 2 3 1 2 3]
A2=[4 5 6 4 5 6 4 5 6
4 5 6 4 5 6 4 5 6
4 5 6 4 5 6 4 5 6]
A3=[7 8 9 7 8 9 7 8 9
7 8 9 7 8 9 7 8 9
7 8 9 7 8 9 7 8 9]
As an alternative to #thewaywewalk's answer and using kron and repmat:
clear
A=[1 4 7;2 5 8;3 6 9];
B = repmat(kron(A',ones(3,1)),1,3);
A1 = B(1:3,:)
A2 = B(4:6,:)
A3 = B(7:end,:)
Which results in the following:
A1 =
1 2 3 1 2 3 1 2 3
1 2 3 1 2 3 1 2 3
1 2 3 1 2 3 1 2 3
A2 =
4 5 6 4 5 6 4 5 6
4 5 6 4 5 6 4 5 6
4 5 6 4 5 6 4 5 6
A3 =
7 8 9 7 8 9 7 8 9
7 8 9 7 8 9 7 8 9
7 8 9 7 8 9 7 8 9
Or as #Divakar pointed out, it would be advisable to create a single 3D array and store all your data in it (general solution):
n = 3; %// # of times you want to repeat the arrays.
A=[1 4 7;2 5 8;3 6 9];
B = repmat(kron(A',ones(n,1)),1,n);
C = zeros(n,n*size(A,2),3);
C(:,:,1) = B(1:n,:);
C(:,:,2) = B(n+1:2*n,:);
C(:,:,3) = B(2*n+1:end,:);
Try if this fits your needs:
A = [1 4 7;2 5 8;3 6 9];
n = 3; %// size(A,1)
cellArrayOutput = arrayfun(#(x) repmat( A(:,x).',n,n ), 1:size(A,2), 'uni',0)
instead of different variable names, everything is stored in a cell array.
if you insist on different names, I'd recommend to use structs:
A = [1 4 7;2 5 8;3 6 9];
n = 3;
structOutput = struct;
for ii = 1:size(A,2)
structOutput.(['A' num2str(ii)]) = repmat( A(:,ii).', n, n );
end
which gives you:
>> structOutput.A1
ans =
1 2 3 1 2 3 1 2 3
1 2 3 1 2 3 1 2 3
1 2 3 1 2 3 1 2 3
and so on.
I don't expect to much performance plus, you should share your full code for further help.

Ismember by row in MATLAB

I'm trying to figure out a matrix-oriented way to perform the ismember function by row in MATLAB. That is, if I have matrices
[1 2 3 4 5 6]
[7 8 9 10 11 12]
And I put in
[3 4 5]
[10 11 12]
Into some ismember-ish function, I'd like it to return
[0 0 1 1 1 0]
[0 0 0 1 1 1]
Other than looping over each row of the matrix in a for loop, is there a way to do this?
Assuming that your data are available as matrices A and B
A = [
1 2 3 4 5 6
7 8 9 10 11 12
];
B = [
3 4 5
10 11 12];
you can convert them to cells and then use cellfun
cellA = mat2cell(A, ones(1, size(A,1)), size(A,2));
cellB = mat2cell(B, ones(1, size(B,1)), size(B,2));
membership = cell2mat(cellfun(#ismember, cellA, cellB, 'UniformOutput', false));
This returns
membership =
0 0 1 1 1 0
0 0 0 1 1 1
A = [5 3 4 2]; B = [2 4 4 4 6 8];
[Lia1,Locb1] = ismember(A,B)
Lia1 =
1 1 1 1 0 0
Locb1 =
4 3 3 3 0 0

Find the maximum value from the each set

I have 3 sets of data as shown in below:
A=[3 1 4 2;7 9 8 3;7 5 3 6;4 1 9 3]
B=[1 0 4 5;7 7 1 3;4 7 6 5;2 2 1 9]
C=[9 1 3 7;9 6 5 5;1 4 3 2;0 3 2 1]
I need to find out the maximum value when comparing with each other.
for instance, for matrix [1x1] from each set,
A=3,B=1,C=9,thus maximum number is 9
for matrix [1x2],maximum value=1 and so on..
so the
final result =[9 1 4 7;9 9 8 5;7 7 6 6;4 3 9 9]
Any suggestion to solve this problem? Thanks!
You can use max. For the case with 3 matrices, just use
max(A, max(B, C))
If you have more than three matrices, writing those max statements can get tiring, so you would use cat before taking the maximum
max(cat(3, A, B, C, D, E), [], 3)
You could do like this:
A=[3 1 4 2;7 9 8 3;7 5 3 6;4 1 9 3];
A = reshape(A,[1,numel(A)]);
B=[1 0 4 5;7 7 1 3;4 7 6 5;2 2 1 9] ;
B = reshape(B,[1,numel(B)]);
C=[9 1 3 7;9 6 5 5;1 4 3 2;0 3 2 1];
C = reshape(C,[1,numel(C)]);
D = [A;B;C];
for ii = 1:size(D,2)
res(1,ii) = max(D(:,ii));
end
res = reshape(res,[4,4]);

Matlab center a matrix to its mean

I want to centre a matrix to its mean,
A[i][j] = A[i][j]-mean(A,j)
So I subtract from each point the mean of the according column.
I could not find a function to centre my data, and it is not very straightforward to create my own
>> A=[1 4 7;2 5 8;3 6 9]
A =
1 4 7
2 5 8
3 6 9
>> A-repmat(mean(A),size(A,1),1)
ans =
-1 -1 -1
0 0 0
1 1 1
A = bsxfun(#minus,A,mean(A));
for example:
A = magic(5);
A = bsxfun(#minus, A, mean(A))
A =
4 11 -12 -5 2
10 -8 -6 1 3
-9 -7 0 7 9
-3 -1 6 8 -10
-2 5 12 -11 -4

Replace duplicate elements from vector with 0 (Matlab/Octave)

I want to replace duplicate elements from a vector with 0, and keep only the first occurrence.
If I have a vector like
[ 1 1 2 2 2 3 3 3 4 4 4 4 5 5 5 5 6 6 6 ]
how could I transform it into
[ 1 0 2 0 0 3 0 0 4 0 0 0 5 0 0 0 6 0 0 ] ?
Thanks.
a = [ 1 1 2 2 2 3 3 3 4 4 4 4 5 5 5 5 6 6 6 ];
[c, ia] = unique(a, 'first');
t = a;
t(ia) = 0;
filtered_vect = a - t;
edit: That in a more concise way, destroying the original vector:
a = [ 1 1 2 2 2 3 3 3 4 4 4 4 5 5 5 5 6 6 6 ];
[c, ia] = unique(a, 'first');
a(~ismember(1:length(a),ia)) = 0;