Four indexing matrix

I have a matrix nxn, each element is described by a 2 index number (i.e. one number for the column, one for the row). The problem is that i want to access this matrix by a four digit indexing. Is there a way to do that?

댓글 수: 3

Oleg Komarov
Oleg Komarov 2011년 7월 21일
What do you mean? You want to retrieve two points at the same time?
Nikolaos
Nikolaos 2011년 7월 22일
no, i want to access a matrix not by 2 digits indexing but by 4 index indexing. For example i want to map A(k,l,m,n)->p(i,j) {where i==row, j=column of p matrix }
Jan
Jan 2011년 7월 22일
There is an infinite number of possibilities to map "A(k,l,m,n)->p(i,j)". Please specify how {k,l,m,n} and {i,j} are connected.

댓글을 달려면 로그인하십시오.

 채택된 답변

Nikolaos
Nikolaos 2011년 7월 22일

0 개 추천

Ok, i made it. So, for a given matrix "pall" i go to a matrix "alpha(i,j,k,l)". For example: alpha(1,1,1,1)=pall(1,1)=1 alpha(2,2,2,2)=pall(4,4)=16 alpha(1,1,1,2)=pall(1,2)=2 .. p.s. If you have a faster solution please tell me. Thank you all
%this is the actual algorithm % pall=([1 2 3 4;5 6 7 8; 9 10 11 12; 13 14 15 16]); k=0 for n1=1:4 for n2=1:4 k=k+1 temp(k)=pall(n1,n2) end end k=0 alpha=zeros(2,2,2,2) for n3=1:2 for n4=1:2 for n5=1:2 for n6=1:2 k=k+1 alpha(n3,n4,n5,n6)=temp(k) end end end end %

댓글 수: 5

Nikolaos
Nikolaos 2011년 7월 22일
pall=([1 2 3 4;5 6 7 8; 9 10 11 12; 13 14 15 16]);
k=0
for n1=1:4
for n2=1:4
k=k+1
temp(k)=pall(n1,n2)
end
end
k=0
alpha=zeros(2,2,2,2)
for n3=1:2
for n4=1:2
for n5=1:2
for n6=1:2
k=k+1
alpha(n3,n4,n5,n6)=temp(k)
end
end
end
end
Andrei Bobrov
Andrei Bobrov 2011년 7월 22일
is better
permute(reshape(pall,[2 2 2 2]),[2,1,3,4])
Jan
Jan 2011년 7월 22일
@Nikolaos: Please use code-formatting either by starting the code lines with 2 spaces or by marking the code section with the mouse and hitting the "{} Code" button in this page.
Nikolaos
Nikolaos 2011년 7월 22일
Ok thanks a lot.. what needs to be change if for example the pall matrix is larger? fe 16x16
Walter Roberson
Walter Roberson 2011년 7월 22일
Comments cannot be formatted yet, unfortunately.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Friedrich
Friedrich 2011년 7월 21일

0 개 추천

Hi,
I am not sure what you mean with 4digit indexing. Do you want a 4d matrix?
>> a = zeros(3,3,3,3);
>> a(1,2,1,3)
ans =
0
Or do you like to pass 2 pairs of indices?
a = [1 2; 3 4]
%try to access 1,1 and 1,2
ind = sub2ind(size(a),[1 1],[1 2])
a(ind)
ans =
1 2

댓글 수: 1

Nikolaos
Nikolaos 2011년 7월 22일
i want to access a matrix not by 2 digits indexing but by 4 index indexing. For example i want to map A(k,l,m,n)->p(i,j) {where i==row, j=column of p matrix }

댓글을 달려면 로그인하십시오.

Jan
Jan 2011년 7월 22일

0 개 추천

One example:
A = rand(3, 4, 5, 6);
p = reshape(A, 12, 30);
disp(A(2,3,4,5))
disp(p(8, 24))
Does this match your needs? Then SUB2IND and IND2SUB might help.

댓글 수: 5

Nikolaos
Nikolaos 2011년 7월 22일
ok, i am a bit new to matlab, could be so kind of giving me an example using as a matrix the pall=([1 2 3 4;5 6 7 8; 9 10 11 12; 13 14 15 16])?
Andrei Bobrov
Andrei Bobrov 2011년 7월 22일
pall=[1 2 3 4;5 6 7 8; 9 10 11 12; 13 14 15 16]
a = zeros(2,2,2,2)
a(:) = pall
Jan
Jan 2011년 7월 22일
a = reshape(pall, 2,2,2,2);
Nikolaos
Nikolaos 2011년 7월 22일
great thanks a lot
Nikolaos
Nikolaos 2011년 10월 5일
but what if i have a matrix "pall" 100x100, or nxn .. ??
Thanx in andanve.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2011년 7월 21일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by