0

I am trying to use the convhull function in a loop and for that I need to split matrices into submatrices of different sizes. Here is the code I am using:

    x1=data(:,5); % x centre location
    y1=data(:,16); % y centre location
    z1=phi*90; % phi angle value
    n=300;
    %Create regular grid across data space
    [X,Y] = meshgrid(linspace(min(x1),max(x1),n), linspace(min(y1),max(y1),n));

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%% PLOT USING SCATTER - TRYING TO ISOLATE SOME REGIONS %%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    c=z1>10 & z1 < 20;
    c=c.*1;
    j=1;
    for i=1:length(z1)
        if z1(i)< 20 && z1(i)> 10
            c(i) = 1;
        else
            c(i)= 0;
        end       
    end


    C=[c c c];
C = ~C;

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);



 for i = 1:length(elementalLengthA)
    result(i) = {C(elementalStartA(i):elementalStartA(i)+elementalLengthA(i)-1,:)};
    length(x1(i))=length(cell2mat(result(i)));
    length(y1(i))=length(cell2mat(result(i)));
end

My for loop doens't work properly and I get this error: ??? Subscript indices must either be real positive integers or logicals.

My matrix C is an nx3 matrix made of lines of 1 and 0. With the result(i) line I am splitting the C matrix into submatrices of 1. Let's say

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;] 

Then

>> 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   

Now x1 and y1 are two vector column nx1. And I want to split them according to the length of C submatrices. so length(x1(1)) should be 1, length(x1(2))=3, length(x1(3))=2 and same for the y vector. Is it possible to do that?

EDIT: Just to make it more clear For instance

x1 =

     1
     2
     3
     4
     5
     6
     7
     8
     9

and

y1 =

     2
     4
     6
     8
    10
    12
    14
    16
    18

I want to get this as an output:

x1(1)=[1], x1(2)=[4 5 6]' and x1(3)=[8 9]'
y1(1)=[2], y1(2)[8 10 12]' and y1(3)=[16 18]'

Thanks Dorian

6
  • you cannot impose length function to the left side of assignment. thats the error. whats your initial value of X1 & Y1.. after splitting X1 & Y1 what do you think the result might look like? Apr 2, 2015 at 15:24
  • For instance in my real code when I do length(x1) I get 96616 length(cell2mat(result(1)))=354 and I want to index the vector x1 to have length(x1(1))=354 too Let me edit the question Apr 2, 2015 at 15:26
  • what is the value of data? Apr 2, 2015 at 15:29
  • do you wanna split X into X1, X2.. according to the length of result(1), result(2)... ?? Apr 2, 2015 at 15:33
  • I edited the question with an example Apr 2, 2015 at 15:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.