Filter löschen
Filter löschen

How do I switch specific elements in an index?

22 Ansichten (letzte 30 Tage)
hbcukid
hbcukid am 27 Apr. 2021
Kommentiert: Matt J am 4 Mai 2021
I have a 3000x7000 2d matrix
a = randi(20,3000,7000)
and I am trying to switch elements of specific indices, but don't know how. I currently have a csv with a column of the original index and another one for the index I would like to switch it to, should I change it to a txt file? I was looking for other posts similar to what I am searching for on here but I couldn't find anything quite right. What are the best practices for that strategy? Would it be a look up table? If so is it better to use a txt file or excel file?
EX: smaller sample size
x = randi(20,20);
xlsx:
original swap output
(index:value): (index:value): (index:value):
1:1 11:11 1:11
2:2 12:12 2:12
3:3 13:13 3:13
4:4 14:14 4:14
(output column so you can understand expected result)
Input:
1 2 3 4 ... 11 12 13 14
1 1 2 3 4 11 12 13 14
2 5 6 7 8
3 3 2 4 5
4 6 7 8 16
5 11 10 12 7
Expected Ouput: (x denotes other value from a different index in xlsx)
1 2 3 4 ... 11 12 13 14
1 11 12 13 14 x x x x
2 1 10 12 8
3 3 2 4 5
4 6 7 8 16
5 11 10 12 7
Edit: I am trying to achieve this but on a larger scale so I can work with larger datasets

Akzeptierte Antwort

Matt J
Matt J am 27 Apr. 2021
Bearbeitet: Matt J am 27 Apr. 2021
The best practice is to index the matrix and assign to those indices, as in the following example,
x = zeros(5,5);
indices=[1,4,8,11];
newvalues=[30,60,20,17];
x(indices)=newvalues
x = 5×5
30 0 17 0 0 0 0 0 0 0 0 20 0 0 0 60 0 0 0 0 0 0 0 0 0
  13 Kommentare
hbcukid
hbcukid am 4 Mai 2021
Bearbeitet: hbcukid am 4 Mai 2021
Last case I promise!
A = [1 2 3 0; 4 5 6 0; 7 8 9 0; 3 6 9 0]
A = 4×4
1 2 3 0
4 5 6 0
7 8 9 0
3 6 9 0
B = [10 11 12 0; 13 14 15 0; 16 17 18 0; 12 14 17 0]
B = 4×4
10 11 12 0
13 14 15 0
16 17 18 0
12 14 17 0
How would I get the output:
B = 4x4
1 2 3 0
1 2 3 0
3 6 9 0
7 8 9 0
([1,1,4,3])
Would it be the same thing?
Matt J
Matt J am 4 Mai 2021
That would be,
B = A([1,1,4,3],:)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Cell Arrays 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