How do I switch specific elements in an index?

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

 Accepted Answer

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 Comments

Here is an extension of the above example to the case where your indices are given in the form of subscripts (i,j)
x = zeros(5,5);
indices=[1,1;
4,1;
3,2;
1,3];
newvalues=[30,60,20,17];
linearIndices=sub2ind( size(x), indices(:,1), indices(:,2) ).'
linearIndices = 1×4
1 4 8 11
x( linearIndices )=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
What about in the case when I have a large dataset? It would be tedious to go through and write that for 1000+ indeces which is why I was trying to do it from a data import for mass cases
Matt J
Matt J on 27 Apr 2021
Edited: Matt J on 27 Apr 2021
There's nothing in my proposal which assumes you will write the indices by hand. You just need to get them into a Matlab vector somehow. By all means, import them if you already have them tabulated somewhere.
I'm having trouble translating that for indices. I get what you mean in the case of values but how does that work if I am just swapping random indices?
I'm not sure I understand the distinction you're trying to draw. If you are just trying to copy values from one location in an array to another, it is very much the same kind of indexing operation, e.g.
x=randi(100,1,7)
x = 1×7
3 90 9 46 84 9 81
x([1,3,5])=x([2,6,7])
x = 1×7
90 90 9 46 81 9 81
Oh I get it now!
If I'm doing it for a large dataset would I have to go through and put a comma in one by one for all thousand?Also, how would I do that for x instead of y?
No, you can do things like this:
a = randi(20,3000,7000);
a(1:1000,:)=a(end-999:end,:)
Sorry I didn't see you responded. I tried to do it recursively but it only does 1 column.
Ex:
a = randi(10,10,10);
b = zeros(10,10)
for i = 1:length(a)
b([9,4,2,8,3,6,7,1,5,10]) = a([9,4,2,8,3,6,7,1,5,10])
end;
What is supposed to be accomplished in the example?
I am trying to get the values between the two to switch throughout the whole matrix
A = [1 2 3 0; 4 5 6 0; 7 8 9 0]
B = [10 11 12 0; 13 14 15 0; 16 17 18 0]
for i = 1:length(A)
B([2,4,3,1]) = A([2,4,3,1])
end;
output:
B = [2 0 3 1; 5 0 6 4; 8 0 9 7]
A = [1 2 3 0; 4 5 6 0; 7 8 9 0]
A = 3×4
1 2 3 0 4 5 6 0 7 8 9 0
B=A(:,[2,4,3,1])
B = 3×4
2 0 3 1 5 0 6 4 8 0 9 7
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?
That would be,
B = A([1,1,4,3],:)

Sign in to comment.

More Answers (0)

Asked:

on 27 Apr 2021

Commented:

on 4 May 2021

Community Treasure Hunt

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

Start Hunting!