%# create an image with one white pixel
img = zeros(100);
img(25,65) = 1;
%# show the image
figure
imshow(img);
%# now circle the pixel. To be sure of the coordinate, let's run find
[x,y] = find(img);
hold on
%# plot a red circle...
plot(x,y,'or')
%# ... and it's not in the right place
%# plot a green circle with x,y switched, and it works
plot(y,x,'og')
Edit 1
数组维度
变量至少有两个维度。标量是大小[1,1],向量是大小[1,n] or [n,1]. Thus, ndims对于其中任何一个都返回 2(事实上,ndims([])也是 2,因为size([]) is [0,0])。这使得测试输入的维度变得有点麻烦。要检查一维数组,您必须使用isvector, 0D 数组需要isscalar.
Edit 2
数组赋值
通常,Matlab 对数组赋值非常严格。例如
m = magic(3);
m(1:2,1:3) = zeros(3,2);
throws a
??? Subscripted assignment dimension mismatch.
然而,这些工作:
m(1:2,1:2) = 1; %# scalar to vector
m(2,:) = ones(3,1); %# vector n-by-1 to vector 1-by-n (for newer Matlab versions)
m(:) = 1:9; %# vector to 'linearized array'
Edit 3
数组大小错误的逻辑索引祝调试顺利!
逻辑索引似乎调用find,因为您的逻辑数组不需要与索引相同数量的元素!
>> m = magic(4); %# a 4-by-4 array
>> id = logical([1 1 0 1 0])
id =
1 1 0 1 0
>> m(id,:) %# id has five elements, m only four rows
ans =
16 2 3 13
5 11 10 8
4 14 15 1
%# this wouldn't work if the last element of id was 1, btw
>> id = logical([1 1 0])
id =
1 1 0
>> m(id,:) %# id has three elements, m has four rows
ans =
16 2 3 13
5 11 10 8