From: dale on
Basically, i'm new to matlab and I am faced with a loopiing problem where the output variable,i.e my answer does not loop properly. The wrong answer I get is:


M =

46.4348

But I desire :

M =
46.4348
43.8353

I cannot seem to get the 43.8353. The full code is below and any help appreciated. thanks.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc;
clear all;

M = zeros(1)


exts = [ 2 1 3 2;
7 1 10 2] % ext is the extends of the region


[ m n ] = size(exts) ;

len = m


for i = 2:3
for j = 1:2
% for i = 7:10
% for j =1:2


G= [ 7.2111 17.2627
11.4018 16.5529
12.6491 5.8310
16.4924 13.0384
21.0238 12.6491
27.8927 21.0238
12.0000 30.8869
6.3246 20.8806
8.2462 8.6023
5.0990 12.1655 ];



L = [ 0 0
1 1
1 1
0 1
0 0
0 0
2 0
2 0
2 0
2 2 ]

for k = 1:len
if (L(i,j) ==k)


M = M + G(i,j)

end

end

end

end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%







From: TideMan on
On Aug 6, 10:44 am, "dale " <persa...(a)gmail.com> wrote:
> Basically, i'm new to matlab and I am faced with a loopiing problem where the output variable,i.e my answer does not loop properly. The wrong answer I get is:
>
> M =
>
>                    46.4348
>
> But I desire :
>
> M =
>                    46.4348
>                    43.8353
>
> I cannot seem to get the 43.8353. The full code is below and any help appreciated. thanks.
>
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> clc;
> clear all;
>
> M = zeros(1)
>
>   exts =     [ 2    1     3    2;
>                  7     1     10    2]    % ext is the extends of the region
>
>        [ m n ]  = size(exts)       ;
>
>               len = m
>
>  for i = 2:3
>     for j = 1:2
> %  for i = 7:10
> %     for j =1:2
>
>     G=    [     7.2111   17.2627    
>             11.4018   16.5529
>             12.6491    5.8310  
>            16.4924   13.0384  
>            21.0238   12.6491  
>             27.8927   21.0238  
>             12.0000   30.8869  
>              6.3246   20.8806  
>             8.2462    8.6023
>              5.0990   12.1655  ];
>
>         L = [  0     0  
>                   1     1  
>                   1    1  
>                   0     1  
>                   0     0  
>                   0     0    
>                   2     0    
>                   2     0    
>                   2     0    
>                   2     2     ]
>
>     for k = 1:len
>       if (L(i,j) ==k)
>
>         M = M  + G(i,j)
>
>       end
>
>     end
>
>     end
>
>  end
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

You've defined M as a scalar here:
M = zeros(1)
and that's what Matlab has given you. No problem.

What exactly are you trying to do?
Whatever it is, I'm certain you can dispense with at least two, if not
three, of the for loops by using vector algebra.



From: dale on
The 'exts' matrix are the starting and ending col and row values of the matrix L with regions of 1 and 2 . So basically, the first row in 'exts' are the extents of region with 1's and the 2nd row in 'exts' are the extents of region with the 2's.

I am trying to sum up all the values in G that are defined by the regions.

Any suggestions would help

dale
From: TideMan on
On Aug 6, 11:20 am, "dale " <persa...(a)gmail.com> wrote:
> The 'exts' matrix are the starting and ending col and row values of the matrix L with regions of 1 and 2 . So basically, the first row in 'exts' are the extents of region with 1's  and the 2nd row in 'exts' are the extents of region with the 2's.
>
> I am trying to sum up all the values in G that are defined by the regions..
>
> Any suggestions would help
>
> dale

Yes, it's as clear as mud now.

Perhaps if you tidy the code up a bit, it will become clear.
1. Take the the definitions of G and L outside the loops.
2. Preallocate M as M=zeros(len,1);
3. Take the loop on k outside the other two.
4. Replace for i= with for indx=ext(k,1):ext(k,3)
5. Replace for j= with for jndx=ext(k,2):ext(k,4)
6. Replace M= with M(k)=M(k) + G(indx,jndx);