简体   繁体   中英

Subscripted assignment dimensions mismatch error 4

I want to add(or perform some other function 'fun') on the distinct blocks(sub image) of the full size image using my own myblockproc function(code is given below). Here I have used a small matrix of dim 5,5,4 for testing purpose, actually i have to perform a function(not addition) on four large sized images of m,n dimension dimension of my actual image is m,n,4.

I'm getting this error:

Subscripted assignment dimension mismatch.

Error in myblockproc (line 30)
    blk(:,:,k)=tmp(tc  :    tc+a-1   , tr    :    tr+b-1);

Error in testmyblock (line 19)
CR = myblockproc(I,3,3);

Here is my code on my test matrix of dim 5,5,4. Block size is 2X2.

function [J] = fun(I)
J=I(:,:,1)+I(:,:,2)+I(:,:,3)+I(:,:,4);
end

function [J] = myblockproc(I,r,c)
[m,n,p]=size(I);
ro=ceil(m/r);
cl=ceil(n/c);
Rr=mod(m,r);
Rc=mod(n,c);
blk=zeros(r,c,p);
for i= 1:ro    
a=r;    
    if i==ro            
        a=Rr;         
    end        
tc=((i-1)*r)+1;    
for j=1:cl                
    b=c;
    if j==cl            
        b=Rc;         
    end                        
    tr=((j-1)*c)+1;

    for k=1 : p
    tmp=I(:,:,k)
    blk(:,:,k)=tmp(tc  :    tc+a-1   , tr    :    tr+b-1);
    end
    J=fun(blk); 
end
end

function [CR] = testmyblock()

I(:,:,1)=[1 2 3 4 5 ;6 7 8 9 10 ;11 12 13 14 15; 16 17 18 19 20; 21 23 23 24 25];
I(:,:,2)=[1 2 3 4 5 ;6 7 8 9 10 ;11 12 13 14 15; 16 17 18 19 20; 21 23 23 24 25];
I(:,:,3)=[1 2 3 4 5 ;6 7 8 9 10 ;11 12 13 14 15; 16 17 18 19 20; 21 23 23 24 25];
I(:,:,4)=[1 2 3 4 5 ;6 7 8 9 10 ;11 12 13 14 15; 16 17 18 19 20; 21 23 23 24 25];

CR = myblockproc(I,3,3);
end

I am not sure if these functions are in the same file, but I expect not, since you probably would have got another error then. Remeber though that the standard way in matlab is that all sub functions should appear below the function calling them.

However, the reason that you get this error is that size( blk(:, :, k) ) ~= size( tmp(tc:tc+a-1, tr:tr+b-1) ) for some k . The problem is hard for me to solve, since I do not know exactly what you are doing and can thus not say how to modify the length of the vectors. I do for example not know if it is blk or tc , tr ,... that is incorrect.

The best way of solving this problem is to use the debugger . Set a break point either on the line where the error occurs, or set the debugger to stop on errors (by typing dbstop error or setting the break point from the menu). Here you can check the size of blk and compare with tc , tr ,...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM