简体   繁体   English

下标的分配尺寸不匹配错误4

[英]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). 我想使用我自己的myblockproc函数(在下面给出代码)在全尺寸图像的不同块(子图像)上添加(或执行一些其他功能“有趣”)。 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. 在这里,我使用了一个昏暗的5,5,4小矩阵进行测试,实际上我必须对m,n的四幅大尺寸图像执行功能(而不是加法),而我的实际图像的尺寸为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. 这是我在昏暗的5,5,4的测试矩阵上的代码。 Block size is 2X2. 块大小为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. 尽管要记住,matlab中的标准方法是所有子函数都应出现在调用它们的函数下方。

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 . 但是,出现此错误的原因是对于某些ksize( blk(:, :, k) ) ~= size( tmp(tc:tc+a-1, tr:tr+b-1) ) 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. 例如,我确实不知道它是blk还是tctr ,...这是不正确的。

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). 在发生错误的行上设置一个断点,或将调试器设置为在发生错误时停止(通过键入dbstop error或从菜单中设置断点)。 Here you can check the size of blk and compare with tc , tr ,... 在这里,您可以检查blk的大小并与tctr ,...进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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