Mas Biru
Mas Biru

Reputation: 29

Matlab Fill zeros matric based on array

So i have this data

F =

     1
     1
     2
     3
     1
     2
     1
     1

and zeros matric

NM =

     0     0     0
     0     0     0
     0     0     0

i have rules, from the lis of array make connection for each variabel, from the F data the connection should be

1&1, 1&2, 2&3, 3&1, 1&2, 2&1, 1&1

each connection represent column and row value on NM matric, and if there is connection the value must be +1

so from the connection above the new matric should be

NNM=
2 2 0
1 0 1
1 0 0

im trying to code like this

[G H]=size(NM)
    for i=1:G
        j=2:G
        if F(i)==A(j)
            (NM(i,j))+1
        else
            NM(i,j)=0
        end
    end
    NNM=NM

but there is no change from the NM matric? what shoul i do?

Upvotes: 1

Views: 46

Answers (3)

Luis Mendo
Luis Mendo

Reputation: 112659

You can use sparse (and then convert to full) as follows:

NM = full(sparse(F(1:end-1), F(2:end), 1));

Upvotes: 2

Siva Srinivas Kolukula
Siva Srinivas Kolukula

Reputation: 1251

list = [1 1 ; 1 2 ; 2 3 ; 3 1 ; 1 2 ; 2 1 ; 1 1 ] ;
[nx,ny] = size(list) ;
NM = zeros(3) ;
for i = 1:nx
    for j = 1:ny
        NM(list(i,1),list(i,2)) = NM(list(i,1),list(i,2)) + 1/2 ;
    end
end

Upvotes: 0

Vahe Tshitoyan
Vahe Tshitoyan

Reputation: 1439

Is this what you are trying to do

F = [1 1 2 3 1 2 1 1];

NM = zeros(3, 3);

for i=1:(numel(F)-1)
    NM(F(i), F(i+1))=NM(F(i), F(i+1))+1;
end

Upvotes: 2

Related Questions