필터 지우기
필터 지우기

How do I switch specific elements in an index?

조회 수: 22 (최근 30일)
hbcukid
hbcukid 2021년 4월 27일
댓글: Matt J 2021년 5월 4일
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

채택된 답변

Matt J
Matt J 2021년 4월 27일
편집: Matt J 2021년 4월 27일
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
hbcukid
hbcukid 2021년 5월 4일
편집: hbcukid 2021년 5월 4일
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 2021년 5월 4일
That would be,
B = A([1,1,4,3],:)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by