Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function 将像素设置为特定值_Function_Matlab_Call - Fatal编程技术网

Function 将像素设置为特定值

Function 将像素设置为特定值,function,matlab,call,Function,Matlab,Call,我在matlab中编写了这个函数,它将具有一定隶属度y=1的像素x的值设置为1,如下所示: function c = core(x, y) tolerance = 0.01; pixels = []; index = 1; for i=1:length(y) for j=1:length(y) if abs(y(i,j)-1) <= tolerance

我在
matlab
中编写了这个函数,它将具有一定隶属度
y
=
1
的像素
x
的值设置为
1
,如下所示:

function c = core(x, y)
        tolerance = 0.01;
        pixels = [];
        index = 1;
        for i=1:length(y)
            for j=1:length(y)
                if abs(y(i,j)-1) <= tolerance
                x(i,j) = 1;
                pixels(index) = x(i,j);
                end
            end
            end
            c = pixels;
       end
其中
F
表示图像

基于此,在
matlab
中编写此代码的正确方法是什么。是的,在
matlab
中,这一行可以简单地写成:

C.('C1') = core(x,y);
但是,问题是,基于上述信息,我的调用脚本将返回什么以及如何返回

是的,作为输出,我总是在
ans
中得到
1
。为什么呢


谢谢。

首先,您在函数右侧传递的所有参数都被视为函数的本地参数,不会在外部更新。因此,要获得更新的图像,请在左侧返回它

其次,您的算法中存在错误:

1-用于循环的
不会扫描所有图像

2.
索引
变量永远不会更新

下面的功能应该可以实现您想要的功能:

function [x,pixels] = core(y)
    tolerance = 0.01;
    pixels = [];
    index = 1;
    for i=1:size(y,1)
        for j=1:size(y,2)
            index = j+i*size(y,2);
            if abs(y(i,j)-1) <= tolerance
            x(i,j) = 1;
            pixels = [pixels index];
            end
        end
    end
end
函数[x,像素]=核心(y)
公差=0.01;
像素=[];
指数=1;
对于i=1:尺寸(y,1)
对于j=1:尺寸(y,2)
指数=j+i*大小(y,2);

如果首先是abs(y(i,j)-1),则在函数右侧传递的所有参数将被视为函数的本地参数,不会在外部更新。因此,要获得更新的图像,请在左侧返回它

其次,您的算法中存在错误:

1-用于
循环的
不会扫描所有图像

2.
索引
变量永远不会更新

下面的功能应该可以实现您想要的功能:

function [x,pixels] = core(y)
    tolerance = 0.01;
    pixels = [];
    index = 1;
    for i=1:size(y,1)
        for j=1:size(y,2)
            index = j+i*size(y,2);
            if abs(y(i,j)-1) <= tolerance
            x(i,j) = 1;
            pixels = [pixels index];
            end
        end
    end
end
函数[x,像素]=核心(y)
公差=0.01;
像素=[];
指数=1;
对于i=1:尺寸(y,1)
对于j=1:尺寸(y,2)
指数=j+i*大小(y,2);
如果abs(y(i,j)-1)
tolerance = 0.01;
x = zeros(size(y));
x((abs(y)-1) <= tolerance) = 1;
pixels = find(x==1);