Assign different values per column using logical indexing

조회 수: 13 (최근 30일)
Jose Pratdesaba
Jose Pratdesaba 2023년 9월 27일
댓글: Dyuman Joshi 2023년 9월 27일
I am new to logical indexing and am having some trouble with a specific issue. I want to use logical indexing to assign a certain value to all values in a column where the condition is true, and then a different value to the next column where another condition is true at the same time. E.g.:
matrix = magic(5); % data matrix
boundary_conditions = [10, 100]; % boundary conditions
matrix(matrix(:, [2,3]) < boundary_conditions) = boundary_conditions; % use logical indexing to set values of time columns
Unable to perform assignment because the left and right sides have a different number of elements.
Essentially, I want to find which values in column 2 are below 10, and which values in column 3 are below 100. Then, those that are in column 2, set them to 10, and those that are in column 3, set to 100. And have that reflected in the original matrix, so only columns 2 and 3 are changed.
For some context:
I am working on a molecular dynamics simulation, so I want to check after each timestep whether a particle has passed the boundary in the x- or y- direction.
Because of this, performance is a necessity, since I am working with ~3,000,000 time iterations. So I want to maximize my efficiency.

채택된 답변

Voss
Voss 2023년 9월 27일
편집: Voss 2023년 9월 27일
matrix = magic(5); % data matrix
boundary_conditions = [10, 100]; % boundary conditions
disp(matrix);
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
% using logical indexing:
idx = matrix(:,2) < boundary_conditions(1);
matrix(idx,2) = boundary_conditions(1);
idx = matrix(:,3) < boundary_conditions(2);
matrix(idx,3) = boundary_conditions(2);
disp(matrix);
17 24 100 8 15 23 10 100 14 16 4 10 100 20 22 10 12 100 21 3 11 18 100 2 9
% another approach using max() function:
matrix = magic(5); % restore original data matrix
matrix(:,2) = max(matrix(:,2),boundary_conditions(1));
matrix(:,3) = max(matrix(:,3),boundary_conditions(2));
disp(matrix);
17 24 100 8 15 23 10 100 14 16 4 10 100 20 22 10 12 100 21 3 11 18 100 2 9
% another approach using max() function:
matrix = magic(5); % restore original data matrix
matrix(:,[2 3]) = max(matrix(:,[2 3]),repmat(boundary_conditions,size(matrix,1),1));
disp(matrix);
17 24 100 8 15 23 10 100 14 16 4 10 100 20 22 10 12 100 21 3 11 18 100 2 9
  댓글 수: 4
Jose Pratdesaba
Jose Pratdesaba 2023년 9월 27일
Ahh I see now, thank you very much!
Voss
Voss 2023년 9월 27일
You're welcome!

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

추가 답변 (1개)

Dyuman Joshi
Dyuman Joshi 2023년 9월 27일
A simpler approach for your task would be
matrix = magic(5) % data matrix
matrix = 5×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
matrix(:,2) = max(matrix(:,2),10)
matrix = 5×5
17 24 1 8 15 23 10 7 14 16 4 10 13 20 22 10 12 19 21 3 11 18 25 2 9
matrix(:,3) = max(matrix(:,3),100)
matrix = 5×5
17 24 100 8 15 23 10 100 14 16 4 10 100 20 22 10 12 100 21 3 11 18 100 2 9
If you have to do this for some specific columns or all the columns (for which manually doing that would be a tedious task) you can integrate a for loop.
%For example
matrix = magic(5)
matrix = 5×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
s = size(matrix,2);
boundary_conditions = randi([10 20],1,s)
boundary_conditions = 1×5
16 19 11 11 20
for k=1:s
matrix(:,k) = max(matrix(:,k),boundary_conditions(k));
end
matrix
matrix = 5×5
17 24 11 11 20 23 19 11 14 20 16 19 13 20 22 16 19 19 21 20 16 19 25 11 20
  댓글 수: 5
Jose Pratdesaba
Jose Pratdesaba 2023년 9월 27일
Thank you very much!!
Dyuman Joshi
Dyuman Joshi 2023년 9월 27일
You are welcome!

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by