How do I switch specific elements in an index?

9 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Matt J
Matt J il 27 Apr 2021
Modificato: Matt J il 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 Commenti
hbcukid
hbcukid il 4 Mag 2021
Modificato: hbcukid il 4 Mag 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?

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by