Matlab determine index position in matrix - matlab

Hin everyone..I have matrix
col = [1 2 3 9 10 15 16 17]
I need to divide A into 3 with length B = [3 2 3],
result required :
col1 = [1 2 3] -> from col(1:3)
col2 = [9 10] -> from col(4:5)
col3 = [15 16 17] -> from col(6:8)
Thank you so much...

mat2cell can be use:
A = [1 2 3 9 10 15 16 17];
B = [3 2 3];
mat2cell(A,1, B)
Result:
{
[1,1] =
1 2 3
[1,2] =
9 10
[1,3] =
15 16 17
}

I assume that lenghts in B match col length, so every element is accounted for. If so, you can do it using simple for loop as follows:
col = [1 2 3 9 10 15 16 17];
B = [3 2 3];
start_idx = 1;
for b = B
col_part = col(start_idx : start_idx+b-1)
start_idx = start_idx+b;
end
Results in:
col_part =
1 2 3
col_part =
9 10
col_part =
15 16 17

Related

How to assemble 2x2x1000 array from four 1000x1 arrays

We have four 771x1 arrays that we want to form a 2x2x771 array.
How to make R from H L N and P?
H = [1 2 3 4 5]';
L = [6 7 8 9 10]';
N = [11 12 13 14 15]';
P = [16 17 18 19 20]';
R = [1 6; 11 16];
R(:,:,2) = [2 7; 12 17];
R(:,:,3) = [3 8; 13 18];
R(:,:,4) = [4 9; 14 19];
R(:,:,5) = [5 10; 15 20];
R
Simple:
R=permute(reshape([H,L,N,P]',2,2,[]),[2 1 3])

Matrix with alternating direction of rows

I want to create a square matrix that takes input n and create a matrix incrementing from 0 up to n^2
eg.
input: n = 2
output = [1 2
4 3]
input = 4
output = [1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13]
n = 4;
output = reshape(1:n^2,n,n)';
output(2:2:end,:) = fliplr(output(2:2:end,:))

Find and replace the rows of an array having repeated number by a fixed given row

I have a matrix having rows with repeated numbers. I want to find those rows and replace them with a dummy row so as to keep the number of rows of the matrix constant.
Dummy_row = [1 2 3]
(5x3) Matrix A
A = [2 3 6;
4 7 4;
8 7 2;
1 3 1;
7 8 2]
(5x3) Matrix new_A
new_A = [2 3 6;
1 2 3;
8 7 2;
1 2 3;
7 8 2]
I tried the following which deleted the rows having repeated numbers.
y = [1 2 3]
w = sort(A,2)
v = all(diff(t,1,2)~=0|w(:,1:2)==0,2) % When v is zero, the row has repeated numbers
z = A(w,:)
Can you please help?
bsxfun based solution -
%// Create a row mask of the elements that are to be edited
mask = any(sum(bsxfun(#eq,A,permute(A,[1 3 2])),2)>1,3);
%// Setup output variable and set to-be-edited rows as copies of [1 2 3]
new_A = A;
new_A(mask,:) = repmat(Dummy_row,sum(mask),1)
Code run -
A =
2 3 6
4 7 4
8 7 2
1 3 1
7 8 2
new_A =
2 3 6
1 2 3
8 7 2
1 2 3
7 8 2
You could use the following:
hasRepeatingNums = any(diff(sort(A, 2), 1, 2)==0, 2);
A(hasRepeatingNums,:) = repmat(Dummy_row, nnz(hasRepeatingNums), 1);
See if this works for you,
A= [ 2 3 6;
4 7 4;
8 7 2;
5 5 5;
1 8 8;
1 3 1;
7 8 2 ];
Dummy_row = [1 2 3];
b = diff(sort(A,2),1,2);
b = sum(b == 0,2);
b = b > 0;
c = repmat(Dummy_row,sum(b),1);
b = b' .* (1:length(b));
b = b(b > 0);
newA = A;
newA(b,:) = c;
gives,
newA =
2 3 6
1 2 3
8 7 2
1 2 3
1 2 3
1 2 3
7 8 2
Edit
Not much change is needed, try this,
Dummy_row = [1 2 3];
b = sum(A == 0,2);
b = b > 0;
c = repmat(Dummy_row,sum(b),1);
b = b' .* (1:length(b));
b = b(b > 0);
newA = A;
newA(b,:) = c;

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

Find the location and determine the corresponding value of another array having the same location of one array

If
a=[5 8 1 2 6 7 1 4 2 3 7 8];
b=[7 6 3 1 5 4 2 0 1 8 9 4];
then
a1=[1 7 3]
corresponds to a matrix and d should be [3 4 8]
d is the exact location of the corresponding a value. How do I find this value?
As a one-liner:
arrayfun(#(x) b(find(a == x, 1, 'first')), a1)
Try this:
c = []
for i = 1:length(a1)
index = find(a == a1(i));
c = [c, index(1)]
end
d = []
for i = 1:length(c)
d = [d, b(c(i))]
end
output is [3 4 8]
Hope this helps.