Search through a vector and replace numbers at specific indices with the index

2 Ansichten (letzte 30 Tage)
So this question is really hard to ask probably because it is so easy to answer. But I could really use some help. I have a 100 by 1 vector A. For any number in A I want to make sure the number located at its index is that actual number. If it's a different, I would like to replace it with its index.
For example at index 2, we have 1, but if we look at vector A, 2 appears at index 8, I want to replace the 1 at index 2, with 2. Also at index 6, we have 1 but if we scan through vector A, 6 appears at index 11, I would like to replace the 1 at index 6 with 6 and so forth. Thank you in advance!
A=[1 1 3 4 5 1 7 2 9 5 6 12 1 4 3 16 1 9 1 16 3 4 23 10 4 21 18 5 6 12 11 4 27 5 12 12 4 5 3 12 12 12 9 12 34 1 12 34 31 3 8 9 3 54 4 27 3 11 1 60 31 9 9 24 9 12 9 7 23 4 5 1 5 12 1 3 5 1 23 1 24 3 4 9 18 26 7 21 5 9 1 3 5 11 25 4 7 14 23 21]
The answer looks something like this
answer=[1 2 3 4 5 6 7 8 9 10 11 12 1 14 3 16 1 18 1 16 21 4 23 24 25 26 27 5 6 12 31 4 27 34 12 12 4 5 3 12 12 12 9 12 34 1 12 34 31 3 8 9 54 54 4 27 3 11 60 60 31 9 9 24 9 12 9 7 23 4 5 1 5 12 1 3 5 1 23 1 24 3 4 9 18 26 7 21 5 9 1 3 5 11 25 4 7 14 23 21];
The code should be something like this I think, but this is not giving me the answer am looking for.
B=(1:100 ); for v=1:n if B(v)==any(A(v)) A(v)=B(v) end end

Antworten (1)

KSSV
KSSV am 6 Dez. 2017
Bearbeitet: KSSV am 6 Dez. 2017
Instead of replacing, you can striaght away generate a row vector from 1 to 100? This is your output eh? Still, you can do like below:
A=[1 1 3 4 5 1 7 2 9 5 6 12 1 4 3 16 1 9 1 16 3 4 23 10 4 21 18 5 6 12 11 4 27 5 12 12 4 5 3 12 12 12 9 12 34 1 12 34 31 3 8 9 3 54 4 27 3 11 1 60 31 9 9 24 9 12 9 7 23 4 5 1 5 12 1 3 5 1 23 1 24 3 4 9 18 26 7 21 5 9 1 3 5 11 25 4 7 14 23 21];
pos = 1:length(A) ;
% get indices equal positions
idx = A==pos ;
%%If not replace
A(~idx) = pos(~idx) ;
I hope, I got your question correct. :)
  1 Kommentar
puttogether
puttogether am 6 Dez. 2017
Bearbeitet: puttogether am 6 Dez. 2017
I want to replace a number with the index of its location only if there is another number within A equal to that index. The resulting vector looks like this
A=[1 1 3 4 5 1 7 2 9 5 6 12 1 4 3 16 1 9 1 16 3 4 23 10 4 21 18 5 6 12 11 4 27 5 12 12 4 5 3 12 12 12 9 12 34 1 12 34 31 3 8 9 3 54 4 27 3 11 1 60 31 9 9 24 9 12 9 7 23 4 5 1 5 12 1 3 5 1 23 1 24 3 4 9 18 26 7 21 5 9 1 3 5 11 25 4 7 14 23 21];
answer=[1 2 3 4 5 6 7 8 9 10 11 12 1 14 3 16 1 18 1 16 21 4 23 24 25 26 27 5 6 12 31 ... 4 27 34 12 12 4 5 3 12 12 12 9 12 34 1 12 34 31 3 8 9 54 54 4 27 3 11 60 60 31 9 9 24 9 12 9 7 23 4 5 1 5 12 1 3 5 1 23 1 24 3 4 9 18 26 7 21 5 9 1 3 5 11 25 4 7 14 23 21];

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing 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