0

I have an array

A = [3, 4; 5, 6; 4, 1];

Is there a way I could convert all coordinate pairs of the array into linear indices such that:

A = [1, 2, 3]'

whereby (3,4), (5,6), and (4,1) are represented by 1, 2, and 3, respectively. Many thanks!

The reason I need is because I need to loop through array A such that I could make use of each coordinate pairs (3,4), (5,6), and (4,1) at the same time. This is because I will need to feed each of these pairs into a function so as to make another computation. See pseudo code below:

for ii = 1: length(A);
   [x, y] = function_obtain_coord_pairs(A);
   B      = function_obtain_fit(x, y, I);
end

whereby, at ii = 1, x=3 and y=4. The next iteration takes the pair x=5, y=6, etc.

Basically what will happen is that my kx2 array will be converted to a kx1 array. Thanks for your help.

EBH
  • 10,350
  • 3
  • 34
  • 59
oma11
  • 55
  • 7
  • 3
    `A(1,:)` is `[3,4]`. Same with the othres – Ander Biguri Feb 02 '17 at 11:49
  • Possible duplicate of [Linear indexing, logical indexing, and all that](http://stackoverflow.com/questions/32379805/linear-indexing-logical-indexing-and-all-that) – Ander Biguri Feb 02 '17 at 11:51
  • Thanks @Ander Biguri. I see your point. What i want however is another array `A2 = [1, 2, 3]'` which stores all the linear indices of the original array pairs. – oma11 Feb 02 '17 at 12:06
  • `A(A2(1),:)` but I dont see the point. Also, linear indices are a different thing. `A(2)` is `4` – Ander Biguri Feb 02 '17 at 12:39
  • @oma11 but are the linear indices not just `1:length(A)`? – Wolfie Feb 02 '17 at 12:40
  • I have to agree with @AnderBiguri. I'm not sure what is being asked here. There are many resources available for understanding how to index matrices. Also, this looks like a homework problem. – toshiomagic Feb 02 '17 at 13:42
  • 1
    Your edit just made clear that what you want is my first comment. Replace `function_obtain_coord_pairs(A);` with `x=A(i,1); y=A(i,2)`. Also, change the for loop to `i=1:size(A,1)` – Ander Biguri Feb 02 '17 at 14:07
  • @Ander Biguri i appreciate. Please could kindly explain to me why i needed to use size(A, 1) and not length(A)?. Cheers! – oma11 Feb 02 '17 at 16:55
  • You can read that in the documentation. If you are lazy, try it yourself. Check what `size(A,1)` returns and what `length(A)` returns – Ander Biguri Feb 02 '17 at 17:48

2 Answers2

0

Here is a corrected version for your code:

A = [3, 4; 5, 6; 4, 1];
for k = A.'
    B = function_obtain_fit(k(1),k(2),I)
end

By iterating directly on A you iterate over the columns of A. Because you want to iterate over the rows we need to take A.'. So if we just display k it is:

for k = A.'
    k
end

the output is:

k =
     3
     4
k =
     5
     6
k =
     4
     1
Graham
  • 7,431
  • 18
  • 59
  • 84
EBH
  • 10,350
  • 3
  • 34
  • 59
  • Ok!. Thank you all for your suggestions and contributions. It seems what i want is probably not clear enough. I will try to make that clear just now. – oma11 Feb 02 '17 at 13:47
  • @oma11 I have edited the answer to fit your question – EBH Feb 02 '17 at 15:01
0

Adapting your code, what you want was suggested by @Ander in the comments...

Your code

for ii = 1:length(A);
    [x, y] = function_obtain_coord_pairs(A);
    B      = function_obtain_fit(x, y, I);
end

Adapted code

for ii = 1:size(A,1);
    x = A(ii, 1);
    y = A(ii, 2);
    B = function_obtain_fit(x, y, I); % is I here supposed to be ii? I not defined...
end

Your unfamiliarly with indexing makes me think your function_obtain_fit function could probably be vectorised to accept the entire matrix A, but that's a matter for another day!

For instance, you really don't need to define x or y at all...

Better code

for ii = 1:size(A,1);
    B = function_obtain_fit(A(ii, 1), A(ii, 2), I);
end
Wolfie
  • 27,562
  • 7
  • 28
  • 55