Using a vector of indexes to acces a multidimensional vector
1 view (last 30 days)
Show older comments
First i want to say that i'm pretty new with matlab but i'm quite experienced in other programming leanguages.
So I have a structure called n_gram buit with a variable number of cells each containing a n dimension array. If for exemple n=2 i will have a 20x20 array and so on.
i also have a variable that hold the indexes i need to acess on the n dimension array tempInd=cell(1,n) each cell has an array with L elements. In this problem i need to make L acesses into the n-dim array. And the indexes of the cell i need to acess can be found from tempInd like so seq=(tempInd{1}(i),tempInd{2}(i),......,tempInd{n},(i)) with 1<=i<=L. So like this i will have L sequences of n indexes that specify where in the multydim array i should add put L values in tempVal.
The problem is that even if i create the array seq with n elements indicating the indexes for each dimension they are all treated like single linear indexes. So doing
n_gram{p}(seq)=n_gram{p}(seq)+ tempVal(i)
just adds the tempVal(i) to the n linear interpeted indexes
% p here is the idex of the object in consideration, be awere that each element has it's own multydim array all with the same size
%tempInd and tempVal are respectivly a n cell array holding a 1,L array each and a 1,L array holding an integer for each position
dim=[];
for i=1:n
dim(i)=20; %this is going to be the dimension vector i use to generate my multydim array
end
n_gram{p}=zeros(dim); %This will be my Multydimensional Array , it will always be a 20x20x ...x20 n times
for i=1:L
seq=zeros(1,n); %this is the vector i want to use to access the multydim Array...
for j=1:n %here i do the cicle that extracts the n coordinates i need to index a specific position in the array.
seq(j)=tempInd{j}(i);
end
%HERE THE PROBLEM
n_gram{p}(seq)=n_gram{p}(seq)+ tempVal(L); %indexing into the matrix by using the 1,n size seq array doesn't work
end
I thought the best way is to convert the n indexes into linear indexing but the fact that i don't know the value of the n until the funcion is called breaks me.
Answers (1)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!