I am trying to split an nx3 matrix into submatrices in matlab. So my matrix C is of this shape
C =
1 1 1
0 0 0
0 0 0
1 1 1
1 1 1
1 1 1
0 0 0
1 1 1
1 1 1
It either has rows of zeros or rows of ones. I want to split this matrix to only keep matrices of ones. So here for instance there's three 'groups' of ones. so I want to get
C1 =
1 1 1
C2 =
1 1 1
1 1 1
1 1 1
C3 =
1 1 1
1 1 1
However my real matrix is an n by 3, so i don't know where the ones are.
Edit 1:
Now, i need to split x1 and y1 (individual row vectors with same length as C) to x(1), x(2),.. (similarly for y vector) based on how the matrix C was split
Sample Input:
x1 = (1:9)';
y1 = (2:2:18)';
Desired Output:
x(1)=[1], x(2)=[4 5 6]' and x(3)=[8 9]'
y(1)=[2], y(2) =[8 10 12]' and y(3)=[16 18]'
Use find function to the first column to find the indices of 1-elements. Then, calculate the relative distance between any two consecutive elements of the obtained vector to determine the boundary. After that, use matrix indexing to get the blocks of 1-element-matrices. Let me know if you have a better idea.
c=[1 1 1;
0 0 0;
0 0 0;
1 1 1;
1 1 1;
1 1 1;
0 0 0;
1 1 1;
1 1 1;]
b=c(:, 1);
find(b);
I will leave the rest to yourself.
Input:
c = [1 1 1;
0 0 0;
0 0 0;
1 1 1;
1 1 1;
1 1 1;
0 0 0;
1 1 1;
1 1 1;]
x1 = (1:9)';
y1 = (2:2:18)';
Code:
elementalLengthA = cellfun('length',regexp(sprintf('%i',all(C,2)),'1+','match'));
elementalStartA = regexp(sprintf('%i',all(C,2)),'1+','start');
result = cell(length(elementalLengthA),1);
x = cell(length(elementalLengthA),1);
y = cell(length(elementalLengthA),1);
for i = 1:length(elementalLengthA)
result(i) = {C(elementalStartA(i):elementalStartA(i)+elementalLengthA(i)-1,:)};
x(i) = {x1(elementalStartA(i):elementalStartA(i)+elementalLengthA(i)-1,:)};
y(i) = {y1(elementalStartA(i):elementalStartA(i)+elementalLengthA(i)-1,:)};
end
Output:
>> cell2mat(result(1))
ans =
1 1 1
>> cell2mat(result(2))
ans =
1 1 1
1 1 1
1 1 1
>> cell2mat(result(3))
ans =
1 1 1
1 1 1
>> cell2mat(x(3)) %similarly do it for other cells to get results
ans =
8
9
Related
In Matlab, im looking for a way to solve the following issue in an a bit more elegant way:
In an threedimensional array, on one dimension (i.e. time in my case), from some index on all values equal zero, e.g. the following example array a is 0 for an index of 3 for the second dimension (i.e. a(:,3:end,: == 0)):
a(:,:,1) =
1 1 0
1 0 0
1 0 0
a(:,:,2) =
1 1 0
1 0 0
1 1 0
a(:,:,3) =
1 1 0
1 1 0
1 1 0
[edit, was asked for expected outcome]
expected outcome is:
o(:,:,1) =
1 1
1 0
1 0
o(:,:,2) =
1 1
1 0
1 1
o(:,:,3) =
1 1
1 1
1 1
now of course i could just check for each index in dimension 2, whether it actually is zero everywhere, which is what i'm doing right now, but i feel like there is some better way of solving this issue in matlab in some more elegant way (and possibly even for any multidimensional array). Thanks for helping!
function req = removeColwithSpecVal(a,spec_val)
req=num2cell(a,[1 2]); %Converting to cell keeping the order of rows & columns same
req=vertcat(req{:}); %Stacking multidimensional slices vertically
ind= ~all(req==spec_val,1);%Finding logical indices of the required columns
req = req(:,ind); %The matrix after removal of the not required columns
%Finding the size of the input matrix 'a'; stacking with 1 so that
%it is applicable on 1-D/2-D matrices as well
sz =[size(a) 1];
%Finding the # of columns that the required multi-dimensional matrix will have
sz(2)= numel(req)/prod([sz(1), sz(3:end)]);
%Reshaping to the desired result
req=reshape(permute(reshape(req.', sz(2),sz(1),[]),[2 1 3]),sz);
end
Sample Runs:
%1-D Example
spec_val=5;
a=[1 4 5 2 5];
req = removeColwithSpecVal(a,spec_val)
req =
1 4 2
%2-D Example
spec_val=0;
a=[1 1 0 ;
1 0 0 ;
1 0 0 ];
req = removeColwithSpecVal(a,spec_val)
req =
1 1
1 0
1 0
%Your example (3-D)
spec_val=0;
a(:,:,1) = [1 1 0;
1 0 0;
1 0 0];
a(:,:,2) = [1 1 0;
1 0 0;
1 1 0];
a(:,:,3) = [1 1 0;
1 1 0;
1 1 0];
req = removeColwithSpecVal(a,spec_val)
req(:,:,1) =
1 1
1 0
1 0
req(:,:,2) =
1 1
1 0
1 1
req(:,:,3) =
1 1
1 1
1 1
Also applicable on higher dimensional matrices.
Given a square binary matrix. I want to get all possible binary matrices which are at d Hamming distance apart.
Suppose
A=[1 0 1;
0 1 1;
1 1 0].
Then a matrix which is one (d) Hamming distance apart is
[0 0 1;
0 1 1;
1 1 0].
Any help in Matlab base coding?
I am hoping that I got the definition of hamming weight right in the given context. Based on that hope/assumption, this might be what you were after -
combs = dec2base(0:2^9-1,2,9)-'0'; %//'# Find all combinations
combs_3d = reshape(combs',3,3,[]); %//'# Reshape into a 3D array
%// Calculate the hamming weights between A and all combinations.
%// Choose the ones with hamming weights equal to `1`
out = combs_3d(:,:,sum(sum(abs(bsxfun(#minus,A,combs_3d)),2),1)==1)
Thus, each 3D slice of out would give you such a 3 x 3 matrix with 1 hamming weight between them and A.
It looks like you have 9 such matrices -
out(:,:,1) =
0 0 1
0 1 1
1 1 0
out(:,:,2) =
1 0 1
0 1 1
0 1 0
out(:,:,3) =
1 0 1
0 0 1
1 1 0
out(:,:,4) =
1 0 1
0 1 1
1 0 0
out(:,:,5) =
1 0 0
0 1 1
1 1 0
out(:,:,6) =
1 0 1
0 1 0
1 1 0
out(:,:,7) =
1 0 1
0 1 1
1 1 1
out(:,:,8) =
1 1 1
0 1 1
1 1 0
out(:,:,9) =
1 0 1
1 1 1
1 1 0
Edit
For big n, you need to use loops it seems -
n = size(A,1);
nsq = n^2;
A_col = A(:).';
out = zeros(n,n,nsq);
count = 1;
for k1 = 0:2^nsq-1
match1 = dec2bin(k1,nsq)-'0';
if sum(abs(match1-A_col))==1
out(:,:,count) = reshape(match1,n,n);
count = count + 1;
end
end
Consider an index vector consisting of ones and zeros:
I=[0 0 1 0 1 1 0 0 0];
How can I easily generate the following matrix in matlab:
J=[0 2;
1 1;
0 1;
1 2;
0 3];
Use diff:
I = [0 0 1 0 1 1 0 0 0];
d = diff(I);
ind = [1 find(d~=0)+1]; %// starting index of each new value
rep = diff([ind numel(I)+1]); %// number of repetitions of each new value
J = [ I(ind).' rep.' ];
Using strfind for a slightly bigger example -
I =[1 1 0 0 1 0 1 1 0 0 0 1 1 1 1 0 0]
zero_pos = ['0' num2str(bsxfun(#eq,I,0),'%1d') '0']
ind3 = [ strfind(zero_pos,'01') ; strfind(zero_pos,'10')]
counts = diff(ind3(:))
var = zeros(numel(counts),1);
var(2:2:end)=1;
J = [var counts];
if ind3(1,1)-1>0
J = [1 ind3(1,1)-1;J];
end
Output
J =
1 2
0 2
1 1
0 1
1 2
0 3
1 4
0 2
I have this matrix:
A = [1 3
5 7
9 10];
And this vector:
B = zeros(1,10);
Now I want to change the elements in the ranges of [1:3],[5:7] and [9:10] to 1 .
So, to get this:
C = [1 1 1 0 1 1 1 0 1 1];
I tried:
B(A(:,1):A(:,2)) = 1;
but it just changes the zeros in the first range.
Can it be done without a for loop?
Thanks.
The first column of A are starting positions and the second one are ending positions of each sequence of 1s. To denote a beginning use 1 and for the end -1, then cumsum().
% Preallocate
N = 10;
B = zeros(1,N);
B(A(:,1)) = 1
B =
1 0 0 0 1 0 0 0 1 0
B(A(:,2)+1) = -1
B =
1 0 0 -1 1 0 0 -1 1 0 -1
B = cumsum(B)
B =
1 1 1 0 1 1 1 0 1 1 0
B(1:N)
ans =
1 1 1 0 1 1 1 0 1 1
Would something like this be appropriate?
>> f = #(x)(any(A(:,1)<=x & x<=A(:,2)));
>> i = 1:length(B)
i =
1 2 3 4 5 6 7 8 9 10
>> arrayfun(f,i)
ans =
1 1 1 0 1 1 1 0 1 1
Hello you can try this:
B([A(1,1):A(1,2) A(2,1):A(2,2) A(3,1):A(3,2)]) = 1;
I have to get the unknown matrix by changing the form of a known matrix considering the following rules:
H = [-P'|I] %'
G = [I|P]
where
H is a known matrix
G is an unknown matrix which has to be calculated
I is the identity matrix
So for example, if we had a matrix,
H = [1 1 1 1 0 0;
0 0 1 1 0 1;
1 0 0 1 1 0]
its form has to be changed to
H = [1 1 1 1 0 0;
0 1 1 0 1 0;
1 1 0 0 0 1]
So
-P' = [1 1 1;
0 1 0;
1 1 0]
and in case of binary matrices -P = P.
Therefore
G = [1 0 0 1 1 1;
0 1 0 0 1 0;
0 0 1 1 1 0]
I know how to solve it on paper by performing basic row operations but haven't figured out how to solve it using MATLAB yet.
What is the method for solving the given problem?
If the order of columns in -P' doesn't matter, here's one solution using the function ISMEMBER:
>> H = [1 1 1 1 0 0; 0 0 1 1 0 1; 1 0 0 1 1 0]; %# From above
>> pColumns = ~ismember(H',eye(3),'rows') %'# Find indices of columns that
%# are not equal to rows
pColumns = %# of the identity matrix
1
0
1
1
0
0
>> P = -H(:,pColumns)' %'# Find P
P =
-1 0 -1
-1 -1 0
-1 -1 -1
>> G = logical([eye(3) P]) %# Create the binary matrix G
G =
1 0 0 1 0 1
0 1 0 1 1 0
0 0 1 1 1 1
NOTE: This solution will work properly for integer or binary values in H. If H has floating-point values, you will likely run into an issue with floating-point comparisons when using ISMEMBER (see here and here for more discussion of this issue).