MATLAB - Summing values after reading their position from another matrix - matlab

Suppose I have an array that consists of some elements (A) and I want to take the nth element and sum it with a number of elements that follow it, and I have a matrix that gives me the position of each of the elements from which I should start summing (B).
For example I have
A = [2 3 4 5 6 3 5 7 3 7 3 7 4 7 3 6 2 6 3 8 4 8 4 8 5 8 3]
And I have
B = [8 12 15]
So I would want to sum the value at position B(1,1) which is 8, therefore A(1,8) with the next 4 elements in the matrix
C(1,1) = 7 + 3 + 7 + 3
C(1,2) = 7 + 4 + 7 + 3
C(1,3) = 3 + 6 + 2 + 6

It's always a big fun to use bsxfun:
idx = bsxfun( #plus, B, (0:3)' ); %//' indices of A to be summed together
C = sum( A(idx), 1 );

What I undersdtand from you question is that you want to calculate a matrix C, you only shown an example for calculating C(1,1). C(1,2) will be like,
C(1,2) = 7 + 4 + 7 + 3 + 6 + 2 + 6 + 3 + 8 + 4 + 8 + 4 + 8 + 5 + 8 + 3 = 86
If I'm right my answer is like this
for i = 1:n
C(i) = 0;
for j = B(i):m
C(i) += A(j);
end
end
where
n = B's length (2 in your example)
m = 4 (because you want to add next 4 elements in the matrix)

Related

compare two arrays of different length (case of inequality)

I have two arrays with different length, for example A =[ 2 3 11 0 8 ] and B=[ 2 6 8] ( The data are bigger in the real case) and I want to compare them and find elements that verify abs(A(i)-B(j))> 2 .
Is there any fast function that do that (such ismember but for inequalities) ?
You can create a small function that will check all the possible combinations and send you back the "valid" combination.
A = [2 3 11 0 8];
B = [2 6 8];
C = isbigger(A,B,2); %output = the element that verify abs(A-B)>2
function COMB = isbigger(A,B,val)
[X,Y] = meshgrid(A,B);
X = X(:);
Y = Y(:);
index = abs((X(:)-Y(:)))>val;
COMB = [X(index),Y(index)];
end
OUTPUT:
C =
2 6
2 8
3 6
3 8
11 2
11 6
11 8
0 6
0 8
8 2

index matrix with array

I'd like to index matrix
x=[1:5;6:10]
x =
1 2 3 4 5
6 7 8 9 10
using array
[1,2,1,2,1]
to get
1 7 3 9 5
I tried this:
x([1,2,1,2,1],:)
ans =
1 2 3 4 5
6 7 8 9 10
1 2 3 4 5
6 7 8 9 10
1 2 3 4 5
but that is not what I want. Please help
I'd use linear indexing with sub2ind:
>> v = x(sub2ind(size(x),a,1:5))
v =
1 7 3 9 5
Let
ind = [1, 2, 1, 2, 1];
offset = [1:size(x, 1):numel(x)] - 1;
then
x(ind + offset)
returns what you want. This assumes that your index vector has an entry for every column of x and uses linear indexing to add a column offset to every in-column index.

How can i store results from a loop in a matrix?

I have the following loop
x = [1 2 3 4 5;4 5 6 8 9;8 7 6 3 1;5 6 7 9 1;6 4 2 9 6]
y=[10 30 24 35 40]'
one=[]
for i=1:5
a=i;
ind=[a]
one=x(:,[i])
[b_LS, sigma_b_LS, s_LS] = lscov(one,y)
s = s_LS
aicx1=size(one,1)*log(s)+2*size(one,2)
end
I want to store result as :
A=[ind;aicx1] for example A=[1 2 3 4 5; 26 34 24 325]
You could add at the end of the loop:
x = [1 2 3 4 5; 4 5 6 8 9; 8 7 6 3 1; 5 6 7 9 1; 6 4 2 9 6];
y = [10 30 24 35 40]';
one=[];
for ii=1:5
one = x(:,ii);
[b_LS, sigma_b_LS, s_LS] = lscov(one,y);
s = s_LS;
aicx1 = size(one,1) * log(s) + 2 * size(one,2);
%% Add this
A(1,ii) = ii;
A(2,ii) = aicx1;
end
Notes
Avoid using i or j as variables since they are used for complex numbers
Add an ; at the end of the sentence if you don't want/need the values to appear in the command window

Matrix 1,2,3 how can i generate?

i want to control the creation of random numbers in this matrix :
Mp = floor(1+(10*rand(2,20)));
mp1 = sort(Mp,2);
i want to modify this code in order to have an output like this :
1 1 2 2 3 3 3 4 5 5 6 7 7 8 9 9 10 10 10 10
1 2 3 3 3 3 3 3 4 5 6 6 6 6 7 8 9 9 9 10
i have to fill each row with all the numbers going from 1 to 10 in an increasing order and the second matrix that counts the occurences of each number should be like this :
1 2 1 2 1 2 3 1 1 2 1 1 2 1 1 2 1 2 3 4
1 1 1 2 3 4 5 6 1 1 1 2 3 4 1 1 1 2 3 1
and the most tricky matrix that i'v been looking for since the last week is the third matrix that should skim through each row of the first matrix and returns the numbers of occurences of each number and the position of the last occcurence.here is an example of how the code should work. this example show the intended result after running through the first row of the first matrix.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 (positions)
1 2
2 2
3 3
4 1
5 2
6 1
7 2
8 1
9 2
10 4
(numbers)
this example show the intended result after running through the second row of the first matrix.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 (positions)
1 1 2
2 1 2
3 3 6
4 1 1
5 3
6 1 4
7 2 1
8 1 1
9 2 3
10 4
(numbers)
so the wanted matrix must be filled up with zeros from the beginning and each time after running through each row of the first matrix, we add the new result to the previous one...
I believe the following code does everything you asked for. If I didn't understand, you need to get a lot clearer in how you pose your question...
Note - I hard coded some values / sizes. In "real code" you would never do that, obviously.
% the bit of code that generates and sorts the initial matrix:
Mp = floor(1+(10*rand(2,20)));
mp1 = sort(Mp, 2);
clc
disp(mp1)
occCount = zeros(size(mp1));
for ii = 1:size(mp1,1)
for jj = 1:size(mp1,2)
if (jj == 1)
occCount(ii,jj) = 1;
else
if (mp1(ii,jj) == mp1(ii,jj-1))
occCount(ii,jj) = occCount(ii, jj-1) + 1;
else
occCount(ii,jj) = 1;
end
end
end
end
% this is the second matrix you asked for
disp(occCount)
% now the third:
big = zeros(10, 20);
for ii = 1:size(mp1,1)
for jj = 1:10
f = find(mp1(ii,:) == jj); % index of all of them
if numel(f) > 0
last = f(end);
n = numel(f);
big(jj, last) = big(jj, last) + n;
end
end
end
disp(big)
Please see if this is indeed what you had in mind.
The following code solves both the second and third matrix generation problems with a single loop. For clarity, the second matrix M2 is the 2-by-20 array in the example containing the cumulative occurrence count. The third matrix M3 is the sparse matrix of size 10-by-20 in the example that encodes the number and position of the last occurrence of each unique value. The code only loops over the rows, using accumarray to do most of the work. It is generalized to any size and content of mp1, as long as the rows are sorted first.
% data
mp1 = [1 1 2 2 3 3 3 4 5 5 6 7 7 8 9 9 10 10 10 10;
1 2 3 3 3 3 3 3 4 5 6 6 6 6 7 8 9 9 9 10]; % the example first matrix
nuniq = max(mp1(:));
% accumulate
M2 = zeros(size(mp1));
M3 = zeros(nuniq,size(mp1,2));
for ir=1:size(mp1,1),
cumSums = accumarray(mp1(ir,:)',1:size(mp1,2),[],#numel,[],true)';
segments = arrayfun(#(x)1:x,nonzeros(cumSums),'uni',false);
M2(ir,:) = [segments{:}];
countCoords = accumarray(mp1(ir,:)',1:size(mp1,2),[],#max,[],true);
[ii,jj] = find(countCoords);
nzinds = sub2ind(size(M3),ii,nonzeros(countCoords));
M3(nzinds) = M3(nzinds) + nonzeros(cumSums);
end
I won't print the outputs because they are a bit big for the answer, and the code is runnable as is.
NOTE: For new test data, I suggest using the commands Mp = randi(10,[2,20]); mp1 = sort(Mp,2);. Or based on your request to user2875617 and his response, ensure all numbers with mp1 = sort([repmat(1:10,2,1) randi(10,[2,10])],2); but that isn't really random...
EDIT: Error in code fixed.
I am editing the previous answer to check if it is fast when mp1 is large, and apparently it is:
N = 20000; M = 200; P = 100;
mp1 = sort([repmat(1:P, M, 1), ceil(P*rand(M,N-P))], 2);
tic
% Initialise output matrices
out1 = zeros(M, N); out2 = zeros(P, N);
for gg = 1:M
% Frequencies of each row
freqs(:, 1) = mp1(gg, [find(diff(mp1(gg, :))), end]);
freqs(:, 2) = histc(mp1(gg, :), freqs(:, 1));
cumfreqs = cumsum(freqs(:, 2));
k = 1;
for hh = 1:numel(freqs(:, 1))
out1(gg, k:cumfreqs(hh)) = 1:freqs(hh, 2);
out2(freqs(hh, 1), cumfreqs(hh)) = out2(freqs(hh, 1), cumfreqs(hh)) + freqs(hh, 2);
k = cumfreqs(hh) + 1;
end
end
toc

How do I make sums of sumatrixes in MATLAB without cycle?

I have a large matrix (time x frequency), which I want to reduce partially. I want to sum every 1000 rows (time-samples) together keepinq the frequency information, it is kind of a segmentation.
Is there any way to do it without any cycle in MATLAB?
A smaller example:
M=[1 2 3; 2 3 4; 5 8 7; 5 6 7; 1 2 3; 1 2 4];
and I want to sum every 2 rows together so, that I get:
[3 5 7; 10 14 14; 2 4 7]
Suppose you have a matrix with N rows and M columns and you want to sum every R rows together (where N is divisible by R),
>> mat = [1 2 3; 2 3 4; 5 8 7; 5 6 7; 1 2 3; 1 2 4]
mat =
1 2 3
2 3 4
5 8 7
5 6 7
1 2 3
1 2 4
>> [N, M] = size(mat); %=> [6, 3]
>> R = 2;
The following will allow you to sum groups of R rows:
>> res = reshape(mat, R, [])
res =
1 5 1 2 8 2 3 7 3
2 5 1 3 6 2 4 7 4
>> res = sum(res)
res =
3 10 2 5 14 4 7 14 7
>> res = reshape(res, [], M)
res =
3 5 7
10 14 14
2 4 7
You can also do everything in one line:
>> reshape(sum(reshape(mat, R, [])), [], M)
ans =
3 5 7
10 14 14
2 4 7