Hi lets say that i have matrix size 5x5.
B=[1 2 3 4 5; 10 20 30 40 50; 100 200 300 400 500; 1000 2000 3000 4000 5000; 10000 20000 30000 40000 50000];
How do i use function sum, to sum rows between 2 and 4 and have result:
A = [1110;2220;3330;4440]
You'll find some useful information about matrix indexing in the documentation at http://www.mathworks.co.uk/help/matlab/math/matrix-indexing.html
To illustrate your example, you can use B(2:4,:) to retreive the following:
ans =
10 20 30 40 50
100 200 300 400 500
1000 2000 3000 4000 5000
You can then use the sum function as follows to achieve your desired result:
A = sum(B(2:4,:))
I hope this helps!
All the best,
Matt
MATLAB>> sum(B(2:4,1:4))
ans =
1110 2220 3330 4440
If you want to transpose the result, add ' at the end.
Related
I'm trying to reshape an array but I'm having some issues.
I have an array see image below and I'm trying to get it to look like / follow the pattern in the row highlighted in yellow. (note: I'm not trying to calculate the array but reshape it so it follows a pattern)
aa=[1:5;10:10:50;100:100:500]
aa_new=reshape(aa',[1 numel(aa)])
aa_new produces:
1 2 3 4 5 10 20 30 40 50 100 200 300 400 500
I'm trying to get:
1 2 3 4 5 50 40 30 20 10 100 200 300 400 500
Reverse the column numbers of every second row i.e.
aa(2:2:end,:) = aa(2:2:end, end:-1:1);
Now you're good to go with reshaping:
aa = reshape(aa.', 1, []);
I have a matrix with two columns with about 500 rows. The values of first column vary from 1 to 48. This means that there are repeating numbers in the first column.
I need to make to build a new matrix with 48 rows that each row includes information for a specific number in column 1. For example consider the following data:
x = [ 3 500
5 400
3 200
1 100
1 1100
2 450
3 890
1 110
2 800
....]
So, the out put matrix should be:
Output = [100 1100 110 ...0 0 0; 450 800 ... 0 0 0; 8200 890 0 0 0 ...; 0 0 0];
I know how to do it using for loop but I need to do it without a for loop.
I used the following lines
XX = X(:,2);
Output = XX(X(:,1)==(1:48)');
But it did no work because the number of rows in the new matrix is not the same.
Any help is appreciated.
You can do this with the sub2ind function, I recommend reading the documentation to understand how this works. We'll place all the x(:,2) values into a matrix M. The row that each value belongs in depends on the x(:,1) values, so we must determine which column the value belongs to.
Below j is calculated such that j(k) is equal to the number of times x(k,1) occurs in the vector x(1:k,1). This will be the column we want to place the value x(k,2) into.
x = [ 3 500
5 400
3 200
1 100
1 1100
2 450
3 890
1 110
2 800];
j = sum(triu(x(:,1)==x(:,1)')); % Calculate the column each value should be placed into
M = zeros(max(x(:,1)), max(j)); % Set up the empty matrix
ind = sub2ind(size(M), x(:,1), j(:)); % Get linear indices from subscripts
M(ind) = x(:,2)
M =
100 1100 110
450 800 0
500 200 890
0 0 0
400 0 0
I have a table, which is :
t:([]a:1 3 2 1 2 3 3 2 1;b:10 20 30 40 50 60 70 80 90;c:100 200 300 400 500 600 700 800 900)
And I want all c to be 0 where a is equal to 2, and all be to be 0 where a is equal to 1.
Currently I have these two codes:
t:update b:0 from t where a=1
t:update c:0 from t where a=2
My question is how to combine these two lines of codes into one. Because I am working on a table which is far bigger than this simple example and it will take me a lot of rows of codes to do all the updates, which is too long.
You can use vector conditional for this:
update b:?[a=1;0;b], c:?[a=2;0;c] from t
I have this problem that I need to sort a matrix in MATLAB.
Input:
[20 10 0 50 0;
300 100 50 50 100]
And I'd like the output to be:
[0 0 10 20 50;
50 100 100 300 50]
So I'd like it to sort the first column but the rows remain unchanged. Help is needed!
You did write the answer yourself, try sort
A = [20 10 0 50 0; 300 100 50 50 100];
[A(1,:) idx] = sort(A(1,:),2);
A(2,:) = A(2,idx);
You could also use sortrows and get the desired result in one line:
result = sortrows(A.',1).';
Suppose I have a matrix like:
100 200 300 400 500 600
1 2 3 4 5 6
10 20 30 40 50 60
...
I wish to divide each row by the second row (each element by the corresponding element), so I'll get:
100 100 100 100 100 100
1 1 1 1 1 1
10 10 10 10 10 10
...
Hw can I do it (without writing an explicit loop)?
Use bsxfun:
outMat = bsxfun (#rdivide, inMat, inMat(2,:));
The 1st argument to bsxfun is a handle to the function you want to apply, in this case right-division.
Here's a couple more equivalent ways:
M = [100 200 300 400 500 600
1 2 3 4 5 6
10 20 30 40 50 60];
%# BSXFUN
MM = bsxfun(#rdivide, M, M(2,:));
%# REPMAT
MM = M ./ repmat(M(2,:),size(M,1),1);
%# repetition by multiplication
MM = M ./ ( ones(size(M,1),1)*M(2,:) );
%# FOR-loop
MM = zeros(size(M));
for i=1:size(M,1)
MM(i,:) = M(i,:) ./ M(2,:);
end
The best solution is the one using BSXFUN (as posted by #Itamar Katz)
You can now use array vs matrix operations.
This will do the trick :
mat = [100 200 300 400 500 600
1 2 3 4 5 6
10 20 30 40 50 60];
result = mat ./ mat(2,:)
which will output :
result =
100 100 100 100 100 100
1 1 1 1 1 1
10 10 10 10 10 10
This will work in Octave and Matlab since R2016b.