Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Image coordinates vs plot coordinates</strong> Used to get me every time.</p> <pre><code>%# 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') </code></pre> <p><em>Edit 1</em> </p> <p><strong>Array dimensions</strong></p> <p>Variables have at least two dimensions. Scalars are size <code>[1,1]</code>, vectors are size <code>[1,n]</code> or <code>[n,1]</code>. Thus, <code>ndims</code> returns 2 for any of them (in fact, <code>ndims([])</code> is 2 as well, since <code>size([])</code> is <code>[0,0]</code>). This makes it a bit cumbersome to test for the dimensionality of your input. To check for 1D arrays, you have to use <code>isvector</code>, 0D arrays need <code>isscalar</code>.</p> <p><em>Edit 2</em></p> <p><strong>Array assignments</strong></p> <p>Normally, Matlab is strict with array assignments. For example</p> <pre><code>m = magic(3); m(1:2,1:3) = zeros(3,2); </code></pre> <p>throws a</p> <pre><code>??? Subscripted assignment dimension mismatch. </code></pre> <p>However, these work:</p> <pre><code>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' </code></pre> <p><em>Edit 3</em></p> <p><strong>Logical indexing with wrongly sized arrays</strong> Good luck debugging this!</p> <p>Logical indexing seems to make a call to <code>find</code>, since your logical array doesn't need the same amount of elements as there are indices!</p> <pre><code>&gt;&gt; m = magic(4); %# a 4-by-4 array &gt;&gt; id = logical([1 1 0 1 0]) id = 1 1 0 1 0 &gt;&gt; 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 &gt;&gt; id = logical([1 1 0]) id = 1 1 0 &gt;&gt; m(id,:) %# id has three elements, m has four rows ans = 16 2 3 13 5 11 10 8 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload