0

I have a multidimensional array, Data, and a 1xn vector Location whose elements correspond to the indexes of a specific point I wish to access inside the array Data. Therefore, the number of elements in vector Location is always equal to the dimension of the multidimensional array Data. (If length(Location) = n, then dim(Data)=n).

So for example, if Location = [4 0 9 3], then the point I am trying to access in Data would be: result = Data(4,0,9,3).

If the dimension of Data would be fixed, I could simply do (if say, n=4):

result = Data(Location(1,1), Location(1,2), Location(1,3), Location(1,4));

However, in my problem, the dimension of Data (and therefore the length of Location) change. This makes accessing Data more complicated and I'm a bit lost so as how to do this.

How can I determine the point in Data I am trying to access ?

If possible, I would prefer a solution that doesn't use any loops, but this might be impossible, so if you have a solution based on a loop, please propose it anyway!

Percy
  • 177
  • 1
  • 6
  • Add sample input & expected output for such a multi-dimensional array case? – Divakar Jan 08 '16 at 08:59
  • Doesn't `Data(Location)` work? – BillBokeey Jan 08 '16 at 09:01
  • @BillBokeey It doesn't give me what I want because that returns a new vector whose element correspond to the element numbers in Data. For example: Data [1 , 2 ; 3 , 4] and Location = [1 , 2] Data(Location) returns: result = [1 , 3] whereas what I want is Data(1,2)=2 – Percy Jan 08 '16 at 09:06
  • 1
    Indeed. You need to do : `CellLoc=num2cell(Location);` - `out=Data(CellLoc{:});` – BillBokeey Jan 08 '16 at 09:14
  • 1
    `CellLoc{:}` is equivalent to typing `CellLoc{1},CellLoc{2},...` which is exactly `Location(1),Location(2),...` – BillBokeey Jan 08 '16 at 09:15
  • if possible try to use linear indexing. This will remove the dimension dependence. – patrik Jan 08 '16 at 09:45
  • @patrik linear indexing will be as problematic wouldn't it? I believe `sub2ind` doesn't work for vector indexes – BillBokeey Jan 08 '16 at 09:47
  • @BillBokeey that is true and the reason why linear indexing would be preferred. The solution now requiring a cast to cell and a call `var{:}` to print every element as an expression. In case the vector `Location` would be set up of linear indexing instead, everything would be simple enough. Ultimately this is a design issue. However, this depends on what needs to be done. I know that `ind2sub` takes linear index and converts to subindex. At the point where this is needed the index is probably known anyway. – patrik Jan 08 '16 at 10:06

1 Answers1

3

Make Location a cell array, for the example given in the comment to your question:

Data = [1, 2; 3, 4];
Location = {1, 2};

Data(Location{:})

Which gives

ans = 2

Edit as per comment by @BillBokeey:

Either define Location as a cell array as above or use num2cell to convert the row vector Location to a cell array, for example:

Data = [1, 2; 3, 4];
Location = [1, 2];
Location_cell = num2cell(Location);

Data(Location_cell{:})
smn
  • 548
  • 2
  • 7