I want to evaluate the difference (susbtraction) between all elements in a vector and have the maximum as a result.
For example with
[1 3 7]
the diffs are 2, 4 and 6, so that the result should be 6.
How can I do that with matlab in a clever way?
The maximum difference between any two elements in an array is the difference between the maximum and the minimum value of the array:
x = [1 3 7];
maxDifference = max(x) - min(x)
maxDifference =
6
You can do this.
x = [1 7 3]; % order doesn't matter
MaxDiff = range(x)
Something like this (tested in Octave):
x = [1 3 7];
max(abs(x - [x(2:end) x(1)]))
Related
I have two matrices:
A = [1 2;
3 4;
5 6]
B = A'
The multiplication should take in the way as if row and column vector is extracted from both.
C = B(:,i) * A(i,:) such that for first instance (1st row and 1st column) the result would be:
[1 2;
2 4]
This will be summed up vertically to obtain [3 6]. This sum will give final answer 9. Likewise, 2nd row & 2nd column, 3rd row & 3rd column and so on if matrix size is higher.
This final scalar value will be used for comparing which row and its corresponding column has high yield.
Your required result is actually mathematically equivalent of:
sum(A,2).^2 %or sum(A,2) .* sum(A,2)
If A and B are not transpose of each other then:
sum(A,2).* sum(B,1).'
You can use sum:
result = sum(bsxfun(#times,sum(A,2), B.'),2);
Or in the recent version of MATLAB you can write:
result = sum(sum(A,2).*B.',2)
Previous answer:
You can use permute:
result = sum(reshape(permute(A,[2 3 1]) .* permute(A,[3 2 1]),[],size(A,1)));
Or in the case of A and B:
result = sum(reshape(permute(B,[1 3 2]) .* permute(A,[3 2 1]),[],size(A,1)));
result = [9 49 121]
Thanks to #TommasoBelluzzo and #SardarUsama .
If your Matrix is of Size Nx2, then one possible answer is
A.*A * [1;1] + 2*A(:,1).*A(:,2)
Hey everyone I would like to do the following. I have a vector f.e. [1 2 3 4 6 8] and I want to end up in that vector [1 2 3 4 5 6 7 8] but generally, not like [v(1:4) 5 v(6) 7 v(8)].
Thank you very much!!
If you know your vector is going to be sorted you can use:
a = [1 2 3 4 6 8]; then
a = sort([a,5,7]);
This appends the additional values to the vector, sorts them, and assigns the sorted vector to the original variable.
Since the question is too vague, I'm not sure that I understand correctly, but this is what I came up with
If you have array to be modified
a = [a_1, ..., a_n]
and you want to insert
b = [b_1, ..., b_m]
and the position you want to insert
b_pos = [p_1, ..., p_m]
Then
n = length(a);
m = length(b);
a_pos = setdiff(1:(n+m),b_pos) % find index which is not included in b_pos
c = zeros(1,n+m);
c(a_pos) = a;
c(b_pos) = b;
I wanna delete all the subsets of cell c, suppose I have 6 cell vectors: c{1}=[1 2 3]; c{2}=[2 3 4];
c{3}=[1 2 3 4 5 6]; c{4}=[2 3 4 7]; c{5}=[2 3 7]; c{6}=[4 5 6]; then I wanna delete [1 2 3], [2 3 4] and [4 5 6]. I used two for loops to find all these subsets, but it's too slow for large datasets, is there any simple way can do this?
The following code removes a vector if it's a subset of any other vector. The approach is very similar to my answer to this other question:
n = numel(c);
[i1 i2] = meshgrid(1:n); %// generate all pairs of cells (their indices, really)
issubset = arrayfun(#(k) all(ismember(c{i1(k)},c{i2(k)})), 1:n^2); %// subset?
issubset = reshape(issubset,n,n) - eye(n); %// remove diagonal
c = c(~any(issubset)); %// remove subsets
Note that, in your example, [2 3 7] should also be removed.
You could find the cells that are exact matches for a particular input vector s1 using the following approach:
indx = find(cell2mat(cellfun(#(x)strcmp(num2str(x),num2str(s1)),c,'un', 0)));
You can then loop over matches (which should now be a much smaller set), and remove them by setting their contents to an empty set:
for ii=1:length(indx)
c{:,ii} = [];
end
I have an array (300x6) and I want to use it for this block in simulink. But I have a problem. How can I use "for iterator" block to take the q(i,:), q(i+1,:) , q(i+2,:) ... elements respectively? Or is there any other way to do this idea? Thanks.
In Matlab, for iterates over matrix columns. So you can just transpose to iterate over rows:
A = [1 2 3; 4 5 6; 7 8 9]; %// example matrix
for v = A.'
v = v.'; %// v will be [1 2 3], then [4 5 6] etc
%// Do stuff with v
end
I'm not into Simulink but I guess you can adapt this to that environment.
Is it possible to use matrix entries as indices to other matrix ?
for example :
A=[1 2 ; 4 5 ; 6 7 ];
And I want to reach entries of other matrix using A, without using loops.
Othermat(1,2), Othermat(4,5) %...
If yes how can I do it ?!
Sure, use sub2ind:
A = [1 2; 4 5; 6 7];
ind = sub2ind(size(Othermat),A(:,1),A(:,2));
values = Othermat(ind);
The suggested sub2ind is a natural way to generate indices.
Of course it is also not very hard to find the linear index directly:
A = [1 2; 4 5; 6 7];
Othermat = magic(7);
Othermat(A(:,1)+(A(:,2)-1)*size(Othermat,1))