Matlab Matrix indexing with both column and row - matlab

Suppose I have a 2 by 5 matrix:
d1= 3 3 1 1 2
4 4 2 3 4
and there is a 4 by 5 zero matrix :
z1= 0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
Each column of d1 shows the positions of 1's in the corresponding column in z1. Specifically, I want to get a result like:
r1= 0 0 1 1 0
0 0 1 0 1
1 1 0 1 0
1 1 0 0 1
I am looking for an efficient way to obtain r1 from d1 and z1.

Convert d1 to linear indices, and use them to index z1:
% Prior to R2016b:
I = bsxfun(#plus, d1, (0:size(d1,2)-1) * size(z1,1));
% On or after R2016b:
I = d1 + (0:size(d1,2)-1) * size(z1,1));
% Index using the linearized indices
z1(I) = 1

Related

Putting 1's in certain places

I have 2 matrices
Matrix A = [7 3 5 2 8 4 1 6 9;
5 2 6 1 4 3 9 7 8;
9 1 4 5 2 6 3 6 7;
4 8 1 6 3 7 2 9 5;
6 1 7 2 8 4 5 9 3]
Matrix B = [1 0 0 0 0 0 0 0 0;
0 1 1 0 0 0 0 0 0;
0 0 0 0 0 0 0 1 0;
0 0 0 1 0 1 0 0 0;
0 0 0 0 0 0 0 0 1]
Matrix A and B are already defined.
Here each column can't have more than 1 what i want to do is that if when i do sum for Matrix B if i found 0 in it i have to add 1's in the places of the zero's but in certain places. In each row the 1's have to be placed in certain groups. For example if a 1 is placed in column 1, then it can be placed as well in column 2 or 3 only. It can't be placed anywhere else. If in another row it is placed in column 5, then it can be placed in column 4 or 6 only and so on. It's like group of 3. Each 3 columns are together.
To be more clear:
Here the sum of matrix B is [1 1 1 1 0 1 0 1 1]. The zeros here are placed in column 5 and 7 and i want to add 1 putting in mind where the 1 is going to be placed in the matrix. So in this example the 1 of column 5 can only be placed in row 4 as the 1's in this row are placed in column 4 and 6. The 1 of column 7 can be placed in row 5 or row 3. If we have choice between 2 rows then the 1 will be placed in the placed of the higher number of Matrix A.
The 1's have to be placed in groups; columns 1, 2 and 3 are together, columns 4,5 and 6 are together and columns 7, 8 and 9 are together. so if the 1 is placed in 1 column of the group then it can't be placed in any other place.
Let me simplify it if we have an array like this [0 0 0 0 0 0 0 1 1] This array has 3 categories, columns 1,2 and 3 are 1st category, columns 4,5 and 6 are 2nd category and so on. here i want to place a 1 so that the 3rd category won't have a zero element. This is what i want to do briefly but with a whole matrix with all the categories.
so here the output will be =
[1 0 0 0 0 0 0 0 0;
0 1 1 0 0 0 0 0 0;
0 0 0 0 0 0 0 1 0;
0 0 0 1 1 1 0 0 0;
0 0 0 0 0 0 1 0 1]
This code was tried but it doesn't give the required output as the 1 was placed in the 1st row not in the place where it has to be (the category that it should be in).
sum_cols_B = sum(B); % Sum of Matrix B (dim 1)
[~, idx] = find(sum_cols_B == 0); % Get indices where sum == 0
% Using loop to go through the indices (where sum = 0)
for ii = idx
B(1,ii) = 1; % Insert 1 in the first position of that
end % column in Matrix B
Ask me if the question is still not clear.!
Here's an updated loop that will add the missing 1's:
sum_cols_B = sum(B);
[~, idx] = find(sum_cols_B == 0);
group_size = 3;
for ii = idx
% Calculate the starting column of the group for column ii
% There are (ii-1)/group_size groups
% Add 1 for 1-based indexing
group_start = floor((ii-1)/group_size)*group_size + 1;
% Determine which rows in the current group have nonzero values
group_mask = sum(B(:,group_start:group_start+group_size-1), 2) > 0;
% Find the row number of the max in A column ii corresponding to mask
[~,rownum] = max(A(:,ii).*group_mask);
% The value in column ii of B should have a 1 inserted
% at the row containing the max in A
B(rownum,ii) = 1;
end
Results for B above are:
B =
1 0 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0
0 0 0 1 1 1 0 0 0
0 0 0 0 0 0 1 0 1
B = [1 0 0 0 0 0 0 0 0;...
0 1 1 0 0 0 0 0 0;...
0 0 0 0 0 0 0 1 0;...
0 0 0 1 0 1 0 0 0;...
0 0 0 0 0 0 0 0 1]; % Matrix - using this as an example
sum_cols_B = sum(B); % Sum of Matrix B (dim 1)
[~, idx] = find(sum_cols_B == 0); % Get indices where sum == 0
% Using loop to go through the indices (where sum = 0)
for ii = idx
B(1,ii) = 1; % Insert 1 in the first position of that
end % column in Matrix B

Loopless submatrix assignment in Matlab

I have a matrix F of size D-by-N and a vector A of length N of random integers in the range [1,a]. I want to create a matrix M of size D * a such that each colum M(:,i) has the vector F(:,i) starting from the index (A(i)-1)*D+1 to (A(i)-1)*D+D.
Example:
F = [1 2 3 10
4 5 6 22]
A = [3 2 1 2]
a = 4
M = [0 0 3 0
0 0 6 0
0 2 0 10
0 5 0 22
1 0 0 0
4 0 0 0
0 0 0 0
0 0 0 0]
I can do it with a simple loop
for i = 1 : N
M((A(i)-1)*D+1:(A(i)-1)*D+D,i) = F(:,i);
end
but for large N this might take a while. I am looking for a way to do it without loop.
You can use bsxfun for a linear-indexing based approach -
[D,N] = size(F); %// Get size of F
start_idx = (A-1)*D+1 + [0:N-1]*D*a; %// column start linear indices
all_idx = bsxfun(#plus,start_idx,[0:D-1]'); %//'# all linear indices
out = zeros(D*a,N); %// Initialize output array with zeros
out(all_idx) = F; %// Insert values from F into output array
Sample run -
F =
1 2 3 10
4 5 6 22
A =
3 2 1 2
a =
4
out =
0 0 3 0
0 0 6 0
0 2 0 10
0 5 0 22
1 0 0 0
4 0 0 0
0 0 0 0
0 0 0 0

Linear span of a vector in MATLAB

I'm looking for a way to generate the spans of a given vector in MATLAB.
For example:
if a = [ 0 1 0 1] I need all vectors of the form [0 x 0 y], 1 <= x <= max1, 1 <= y <= max2,.
or if
a = [ 0 1 0 1 1 0] I need all vectors of the form [0 x 0 y z 0], 1 <= x <= max1, 1 <= y <= max2, 1<= z <= max3.
Note that the vector can have a variable number of 1's.
My first impression is that I would need a variable number of for loops, though I don't know if that is doable in MATLAB. Also any other ideas are welcome!
You don't need multiple for loops for this. The code below generates all required vectors as rows of a tall matrix. It actually creates the columns of the matrix one at a time. Each column will have numbers 1:m(i) arranged in the pattern where
each term repeats the number of times equal to the product of all m-numbers after m(i)
the whole pattern repeats the number of times equal to the product of all m-numbers before m(i)
This is what repmat(kron(1:m(i),ones(1,after)),1,before)' does. (Starting with R2015a you can use repelem to simplify this by replacing the kron command, but I don't have that release yet.)
a = [0 1 0 1 1 0];
m = [2 4 3]; // the numbers max1, max2, max3
A = zeros(prod(m), length(a));
i = 1; // runs through elements of m
for j=1:length(a) // runs through elements of a
if (a(j)>0)
before = prod(m(1:i-1));
after = prod(m(i+1:end));
A(:,j) = repmat(kron(1:m(i),ones(1,after)),1,before)';
i = i+1;
end
end
Output:
0 1 0 1 1 0
0 1 0 1 2 0
0 1 0 1 3 0
0 1 0 2 1 0
0 1 0 2 2 0
0 1 0 2 3 0
0 1 0 3 1 0
0 1 0 3 2 0
0 1 0 3 3 0
0 1 0 4 1 0
0 1 0 4 2 0
0 1 0 4 3 0
0 2 0 1 1 0
0 2 0 1 2 0
0 2 0 1 3 0
0 2 0 2 1 0
0 2 0 2 2 0
0 2 0 2 3 0
0 2 0 3 1 0
0 2 0 3 2 0
0 2 0 3 3 0
0 2 0 4 1 0
0 2 0 4 2 0
0 2 0 4 3 0

Assign values w/ multiple conditions

Let's have a M = [10 x 4 x 12] matrix. As example I take the M(:,:,4):
val(:,:,4) =
0 0 1 0
0 1 1 1
0 0 0 1
1 1 1 1
1 1 0 1
0 1 1 1
1 1 1 1
1 1 1 1
0 0 1 1
0 0 1 1
How can I obtain this:
val(:,:,4) =
0 0 3 0
0 2 2 2
0 0 0 4
1 1 1 1
1 1 0 1
0 2 2 2
1 1 1 1
1 1 1 1
0 0 3 3
0 0 3 3
If I have 1 in the first column then all the subsequent 1's should be 1.
If I have 0 in the first column but 1 in the second, all the subsequent 1's should be 2.
If I have 0 in the first and second column but 1 in the third then all the subsequent 1's should be 3.
If I have 0 in the first 3 columns but 1 in the forth then this one should be four.
Note: The logical matrix M is constructed:
Tab = [reshape(Avg_1step.',10,1,[]) reshape(Avg_2step.',10,1,[]) ...
reshape(Avg_4step.',10,1,[]) reshape(Avg_6step.',10,1,[])];
M = Tab>=repmat([20 40 60 80],10,1,size(Tab,3));
This is a very simple approach that works for both 2D and 3D matrices.
%// Find the column index of the first element in each "slice".
[~, idx] = max(val,[],2);
%// Multiply the column index with each row of the initial matrix
bsxfun(#times, val, idx);
This could be one approach -
%// Concatenate input array along dim3 to create a 2D array for easy work ahead
M2d = reshape(permute(M,[1 3 2]),size(M,1)*size(M,3),[]);
%// Find matches for each case, index into each matching row and
%// elementwise multiply all elements with the corresponding multiplying
%// factor of 2 or 3 or 4 and thus obtain the desired output but as 2D array
%// NOTE: Case 1 would not change any value, so it was skipped.
case2m = all(bsxfun(#eq,M2d(:,1:2),[0 1]),2);
M2d(case2m,:) = bsxfun(#times,M2d(case2m,:),2);
case3m = all(bsxfun(#eq,M2d(:,1:3),[0 0 1]),2);
M2d(case3m,:) = bsxfun(#times,M2d(case3m,:),3);
case4m = all(bsxfun(#eq,M2d(:,1:4),[0 0 0 1]),2);
M2d(case4m,:) = bsxfun(#times,M2d(case4m,:),4);
%// Cut the 2D array thus obtained at every size(a,1) to give us back a 3D
%// array version of the expected values
Mout = permute(reshape(M2d,size(M,1),size(M,3),[]),[1 3 2])
Code run with a random 6 x 4 x 2 sized input array -
M(:,:,1) =
1 1 0 1
1 0 1 1
1 0 0 1
0 0 1 1
1 0 0 0
1 0 1 1
M(:,:,2) =
0 1 0 1
1 1 0 0
1 1 0 0
0 0 1 1
0 0 0 1
0 0 1 0
Mout(:,:,1) =
1 1 0 1
1 0 1 1
1 0 0 1
0 0 3 3
1 0 0 0
1 0 1 1
Mout(:,:,2) =
0 2 0 2
1 1 0 0
1 1 0 0
0 0 3 3
0 0 0 4
0 0 3 0

Generate matrix of vectors from labels for multiclass classification (vectorized)

I'm structuring my input for a multiclass classifier (m data points, k classes). In my input, I have the labels for the training data as integers in a vector y (i.e. y is m dimensional and each entry in y is an integer between 1 and k).
I'd like to transform this into an m x k matrix. Each row has 1 at the index corresponding to the label of that data point and 0 otherwise (e.g. if the data point has label 3, the row looks like [0 0 1 0 0 0 0 ...]).
I can do this by constructing a vector a = [1 2 3 4 ... k] and then computing
M_ = y*(1./b)
M = M_ .== 1
(where ./ is elementwise division and .== is elementwise logical equals). This achieves what I want by setting everything in the intermediate matrix that is not exactly 1 to 0.
But this solution seems silly and roundabout. Is there a more direct way that I'm missing?
You can use logical arrays:
M = [1:k] == y;
Given a label vector y such as [1 2 2 1 3 2 3 1] and a number of classes k such as 3, you can convert this to a label matrix Y as follows.
function Y = labelmatrix(y, k)
m = length(y);
Y = repmat(y(:),1,k) .== repmat(1:k,m,1);
The idea is to perform the following expansions:
1 1 1 1 2 3
2 2 2 1 2 3
2 2 2 1 2 3
1 1 1 .== 1 2 3
3 3 3 1 2 3
2 2 2 1 2 3
3 3 3 1 2 3
1 1 1 1 2 3
This yields:
1 0 0
0 1 0
0 1 0
1 0 0
0 0 1
0 1 0
0 0 1
1 0 0
Or just by indexing:
%// Dummy code to generate some input data
y = [1 4 3 7 2 1];
m = length(y);
k = max(y);
%// Actual conversion using y elements as index
M = zeros(m, k);
M(sub2ind(size(M), [1:m], y)) = 1
%// Result
M =
1 0 0 0 0 0 0
0 0 0 1 0 0 0
0 0 1 0 0 0 0
0 0 0 0 0 0 1
0 1 0 0 0 0 0
1 0 0 0 0 0 0