简体   繁体   English

以下Matlab代码的简洁表达是什么?

[英]What's the concise expression for the following Matlab code

img is an image of a single color channel. img是单色通道的图像。 img_temp is defined as follows: img_temp定义如下:

img_temp = zeros(size(img,1), size(img,2), N);

where N is an integer. 其中N是整数。 Therefore, each entry of img_temp contains N values. 因此,img_temp的每个条目都包含N个值。

What's the concise way of expressing the following operation in Matlab? 在Matlab中表达以下操作的简洁方法是什么?

for x=1:size(img,1)
    for y=1:size(img,2)
        img(x,y,1) = find(img_temp(x,y,:)==max(img_temp(x,y,:)));
    end
end

I want to find the index of the largest value in the array img_temp(x,y,:) and store it in the corresponding place in img, namely, img(x,y,1) 我想找到数组img_temp(x,y,:)中最大值的索引,并将其存储在img中的相应位置,即img(x,y,1)

The max function can operate along any dimension and return an index along with the max value. max函数可以沿任何维度运行,并返回索引和最大值。 In your case, you only care about the index and want it stored in img(:,:,1) , so try this: 在你的情况下,你只关心索引并希望它存储在img(:,:,1) ,所以试试这个:

[~, img(:,:,1)] = max(img_temp, [], 3);

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

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