Suppose I have a vector B=[1 1 2 2] and A=[5 6 7 4] in the form of B says the numbers in the A that are need to be summed up. That is we need to sum 5 and 6 as the first entry of the result array and sum 7 and 4 as the second entry. If B is [1 2 1 2] then first element of the result is 5+7 and second element is 6+4.
How could I do it in Matlab in generic sense?
A fexible and general approach would be to use accumarray().
accumarray(B',A')
The function accumulates the values in A into the positions specified by B.
Since the documentation is not simple to understand I will summarize why it is flexible. You can:
choose your accumulating function (sum by default)
specify the positions as a set of coordinates for accumulation into ND arrays
preset the dimension of the accumulated array (by default it expands to max position)
pad with custom values the non accumulated positions (pads with 0 by default)
set the accumulated array to sparse, thus potential avoiding out of memory
[sum(A(1:2:end));sum(A(2:2:end))]
Related
I have a matrix A and a vector b. I don't know their sizes, the size varies because it is the output of another function. What I want to do is filter A by a column (let's say jth column) which has at least one value that is in b.
How do I do this without measuring the size of b and concatenating every filtered result. Right now, the code is like this (assume j is a given value)
bsize=size(b,1);
for i=1:bsize
if i==1
a=A(A(:,j)==b(i),:);
else
a=[a; A(A(:,j)==b(i),:)];
end
end
I want to code a faster solution.
I am adding a numerical example just to make it clear. Let's say
A=[2 4
7 14
11 13
15 14]
and b=[4 14]
What I'm trying to do is filter to obtain the A matrix whose values are 4 and 14 in the second column, the elements of b to obtain the following output.
A=[2 4
7 14
15 14]
In my data A has more than 12000 rows and b has more than 100 elements. It doesn't always have to be the second column, sometimes the column index changes but that's not the problem now.
Use the ismember function to create a logical index based on column j=2 of A and vector b, and use that index into the rows of A:
output = A(ismember(A(:,j), b), :);
I am having problem with MATLAB's maximum function. What I am supposed to do is to replace the maximum value of an array with a number. However, when there are more than one maximal value, the program updates all of them simultaneously. Is there a way to make it do it one by one? The order of replacement is not important; it can be done arbitrarily. The only important thing is to have MATLAB doing it one by one.
Thank you in advance.
The second output of max returns one index:
a=[5,5];
[b,idx]=max(a)
c=b-2;
a(idx)=c
When you say more than 1 maximum value, I assume you are talking about a matrix where max function operates on every column?
You can do the following:
a = [1 1 2;5 5 7; 3 2 9]
Obviously, the maximum value is going to be 9, but if you do the following:
max(a)
The result will be:
5 5 9
Based on each column.
The following may work for you?
max(a(:)) % Maximum value from a matrix (rerranged into 1 column)
you can do the same for the min function.
In MATLAB, we can operate on both rows and columns of a matrix. What does it exactly mean by "row major" or "column major"?
It is important to understand that MATLAB stores data in column-major order, so you know what happens when you apply the colon operator without any commas:
>> M = magic(3)
M =
8 1 6
3 5 7
4 9 2
>> M(:)
ans =
8
3
4
1
5
9
6
7
2
I tend to think "MATLAB goes down, then across". This makes it easy to reshape and permute arrays without scrambling your data. It's also necessary in order to grasp linear indexing (e.g. M(4)).
For example, a common way to obtain a column vector inline from some expression that generates an array is:
reshape(<array expression>,[],1)
As with (:) this stacks all the columns on top of each other into single column vector, for all data in any higher dimensions.
But this nifty syntactic trick lets you avoid an extra line of code.
In MATLAB, arrays are stored in column major order.
It means that when you have a multi-dimensional array, its 1D representation in memory is such that leftmost indices change faster.
It's called column major order because for a 2D array (matrix), the first (leftmost) index is typically the row index, so since it changes faster than the second (next to the right) index, the 1D representation of the matrix is memory correspond to the concatenation of the columns of the matrix.
Say you have a 2D matrix,Is there a particular algorithm or way to do this in excel or matlab that would find the max of each row and column, such that each column and each row has only one maximal number N, where summing all N would result in the largest possible sum,i.e. is a row or column has a max number that is repeated., as such with the simple example below
1 2 4
3 1 4
1 2 4
the out put would be
1 2 4
3 2 4
1 2 4
You are looking for the maximum bipartite matching in a (complete) graph, where your matrix represents the edge weights matrix. You can compute this value using the Hungarian algorithm (MATLAB implementation available for download from File Exchange). Since you want the maximum match, negate all the numbers in your matrix and feed it to this function. You will get back two outputs - one is the (negative of) the maximum sum and the other a binary matrix with ones where the maximal elements occur in each row and column and zeros everywhere else.
I am a new in MATLAB and I have a problem understanding the function size
in this statement: for i=1:size(scale,2) WHERE scale can be any integer number .e.g scale=5.
I found that in MATLAB help size(A,1) returns the number of rows of A , and
size(A,2) returns the number of columns of A.
Now I'm really confused as to what is the functionality of (size).
As you know, matlab deals mainly with matrices. So, the size function gives you the dimension of a matrix depending on how you use it. For example:
1. If you say size(A), it will give you a vector of size 2 of which the first entry is the number of rows in A and the second entry is the number of columns in A.
2. If you call size(A, 1), size will return a scalar equal to the number of rows in A.
3. If you call size(A, 2), size will return a scalar equal to the number of columns in A.
A scalar like scale in your example is considered as a vector of size 1 by 1. So, size(scale, 2) will return 1, I believe.
Hope this clarifies.
The Linear Algebra operations in Matlab/octave by default follow Row-Column order (ie they are row major by default); so if A is a matrix of size 3x2 (3 rows and 2 columns), we can use size to determine the order of matrix/vector
size(A) will return 3 2 (the first entry representing no.of rows & the second one is no.of columns). Similarly,
size(A,1) returns 3 (1 here represents the no. of rows and A has 3 rows)
size(A,2) returns 2 (2 here represents the no. of columns and A has 2 columns)