starting indexing of an array at values greater than 1
37 ビュー (過去 30 日間)
古いコメントを表示
Hi,
I would like to reindex specific arrays to start at given user input values
for example: A = [1,2,3;4,5,6;7,8,9];
start_values = input('Please enter start values in brackets'); >> [3,3]
%some function for index conversion of A to start at 3,3
%then i can reference the matrix this way
display(A(3,3));
ans =
1
%and therefore i could also call
display(A(5,5));
ans =
9
%%%%%%%%%%%%%%%%%%%%%%%%%%%
P.S. this is for correlating local stiffness coefficients to a global stiffness coefficient in the direct stiffness method.
thanks!
0 件のコメント
回答 (2 件)
Stephen23
2015 年 2 月 10 日
編集済み: Stephen23
2015 年 2 月 11 日
Just save the origin-indices in a variable. Then simply subtract them from all the indices after that. You could even make a function to help you do this:
>> A = [1,2,3;4,5,6;7,8,9];
>> X = [3,3]; % origin
>> fun = @(x,y)sub2ind(size(A),1+x-X(1),1+y-X(2));
>> A(fun(3,3))
ans = 1
>> A(fun(5,5))
ans = 9
4 件のコメント
Jan
2015 年 2 月 11 日
I agree with Stephen: Instead of reinventing the indexing methods of the programming language, simply add the required offset to the indices.
David Young
2015 年 2 月 11 日
Matt N asked: "I would actually like to make the re-indexing permanent for each index, so that I can call the values at anytime without the need of the function. Is that possible?"
I think it is possible in one sense - see my answer.
David Young
2015 年 2 月 11 日
All MATLAB arrays are indexed from 1, but an alternative to Stephen Cobeldick's answer is to make an array Ashifted such that the value of A(1,1) is at Ashifted(3,3) and so on. Like this:
% test data
A = [1,2,3;4,5,6;7,8,9];
start_values = [3 3];
% construct shifted matrix
rowStart = start_values(1);
colStart = start_values(2);
Ashifted = NaN(size(A,1)+rowStart-1, size(A,2)+colStart-1);
Ashifted(rowStart:end, colStart:end) = A;
% check if it has worked
disp(Ashifted(3,3)); % same as A(1,1)
disp(Ashifted(4,5)); % same as A(2,3)
If you are happy to lose the original indexing, you can assign Ashifted to A.
Note that this uses extra memory, because Ashifted has extra rows and columns. I've filled them with NaN values to show they're not meaningful data.
Whether it's preferable to use this approach or to add an offset to the indices each time you access A depends on a number of factors, with tradeoffs in memory, time and code complexity.
2 件のコメント
David Young
2015 年 2 月 11 日
Incidentally, I agree with the comments above that overriding subsref is not something worth undertaking.
Stephen23
2015 年 2 月 11 日
編集済み: Stephen23
2015 年 2 月 12 日
Accidentally setting the origin to [1e7,1e7] could be very interesting... this method is good for low indices, but is not a general solution.
- A(1,:) would not return the first data row
- A(1) would not return the first data value
- A(:) is not a vector of all data values
- etc
In fact it completely removes the ability to use the colon to select a whole row/column (x,:), because this would always include the padding values.
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!