1

The ii means the index in the x-axis and the jj means the index value in the y-axis. This example runs over a 2x2 times one-matrix. Does Matlab have some ready commands for the index values ii and jj so I can avoid for-looping?

Input: trying to find a implicit solution but requires the location info (ii,jj)

h=@(a)a+ii+jj; h(ones(2))

Intented output

3 4      1+(1+1)    1+(2+1)
4 5   =  1+(1+2)    1+(2+2)

P.s. Code-smell: Explicit solution, not like this!

hh=ones(2);
for ii=1:2
   for jj=1:2
      hh(ii,jj)=hh(ii,jj)+ii+jj
   end
end 
4

2 回答 2

3
h=@(a)a+ii+jj; 
h(ones(2))

As far as I know Matlab does not have an ii or jj built in like you want. That's not how Matlab works. I suggest you have a look at meshgrid:

Input = ones(2);
[N, M] = size(Input);

[jj, ii] = meshgrid(1:M, 1:N);
hh = jj + ii + Input

meshgrid generates two matrices that can specify row and the column coords of a matrix. I think you're getting too hung up on programming styles from other languages. If you're using Matlab, I suggest you try stick to the native best practice styles.

于 2013-10-24T09:38:23.567 回答
0

I think you are looking for this:

repmat([1 2],2,1)+repmat([1;2],1,2)+ones(2,2)

Suppose you want to generalize it:

myMat = ones(2);

N = size(myMat,1);
M = size(myMat,2);

repmat(1:M,N,1)+repmat((1:N)',1,M)+myMat
于 2013-10-24T09:35:32.810 回答