location of the i'th element

10 Ansichten (letzte 30 Tage)
victor  hanna
victor hanna am 10 Dez. 2019
hi
A is a matrix
Iam using a for loop on all the elements in A like this
n = numel(A);
for i = 1 : n
A(i);
end
but i need the element location in the original matrix
so my quastion is there away to find the i'th elements location in A ?
or if there is another way to run loop on all the elements?
  2 Kommentare
Stephen23
Stephen23 am 10 Dez. 2019
"i need the element location in the original matrix"
And you have got it already: i is its linear index.
"is there away to find the i'th elements location in A"
The linear index of the i-th element of A is i.
Adam
Adam am 10 Dez. 2019
What do you mean by 'location'? i is its location as a 1d index.
You can use
doc ind2sub
to convert to 2d subscripts if you want, or just do the double loop around both dimensions. Or preferably don't do a loop at all, but that would depend entirely what you are actually doing in the loop body.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH am 10 Dez. 2019
Bearbeitet: JESUS DAVID ARIZA ROYETH am 10 Dez. 2019
you can use nested loops
for i = 1 : size(A,1)
for j=1:size(A,2)
A(i,j)
end
end
with a loop:
n = numel(A);
for i = 1 : n
[row,col]=ind2sub(size(A),i);
A(row,col)
end
  2 Kommentare
victor  hanna
victor hanna am 10 Dez. 2019
thanks
i wonder if there is away using one for
JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH am 10 Dez. 2019
n = numel(A);
for i = 1 : n
[row,col]=ind2sub(size(A),i);
A(row,col)
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by