Manipulate elements in a matrix based on a mathematical condition using logical indexing

4 views (last 30 days)
I am having trouble figuring out how to manipulate and reduce a matrix based on a mathematical condition.
Let's say I have a 26x500 matrix named A. After a certain number of columns, a lot of the values in A can be considered redundant, as the value for each row in that column becomes very similar, if not identical, to the value in the first row of that column. Therefore, I would like to implement a conditional statement that basically says if the difference between the value in the last row and the first row is <0.01, to replace all the rows in that column to the value in the first row. Something like this:
A = zeros(26,500); % zeros() just used for illustration
condition = ( A(26,:) - A(1,:) ) < 0.01;
A(:,condition) = A(1,:);
However, the problem that I am running into is when manipulating the original matrix with the condition, the first x amount of columns gets removed due to the false return from the logical array created by the condition and the last statement does not run. I know that when manipulating an array on conditions a statement in the form of
A(A<5) = 23;
A(A>10) = 0;
A(isnan(A)) = 0;
% etc.
However I cannot see how I can implement this form due to the mathematical expression indicating the condition. I also understand that I could create a new matrix from the result of the condition, or use a for loop to iterate through each row and replace the values manually based on what the condition found, but I was more interested in using the logical indexing approach to manipulate the original matrix to be used in later applications.
Can someone shed some light on possible methods and solutions?

Accepted Answer

Voss
Voss on 18 Feb 2024
Sounds like what you're going for is:
condition = abs( A(end,:) - A(1,:) ) < 0.01;
A(:,condition) = A(ones(end,1),condition);
  1 Comment
Bryce Jeffrey
Bryce Jeffrey on 18 Feb 2024
This worked, thank you so much! I am struggling to understand how the ones(end,1) is affecting it the desired way, but I will look into it more.

Sign in to comment.

More Answers (1)

VBBV
VBBV on 18 Feb 2024
A = rand(26,500) % zeros() just used for illustration
A = 26×500
0.9110 0.4958 0.9362 0.0277 0.3503 0.0490 0.2058 0.6486 0.8150 0.4790 0.4769 0.6323 0.7181 0.9758 0.0041 0.8161 0.1898 0.8647 0.3349 0.0844 0.4387 0.9578 0.7741 0.7398 0.3668 0.3572 0.6446 0.4606 0.7653 0.3348 0.1571 0.4911 0.3942 0.5895 0.5081 0.3949 0.5259 0.3585 0.5810 0.2583 0.6082 0.9690 0.7460 0.1886 0.3960 0.5368 0.1031 0.4950 0.0280 0.3748 0.1248 0.3849 0.0910 0.4435 0.9037 0.6677 0.9168 0.1054 0.5385 0.7585 0.9439 0.1228 0.9554 0.2019 0.9193 0.7410 0.9973 0.3996 0.3005 0.7954 0.1190 0.3300 0.6526 0.5445 0.0548 0.0005 0.5258 0.4538 0.7475 0.9178 0.3609 0.8522 0.5712 0.1829 0.3524 0.9947 0.2686 0.5587 0.9179 0.6981 0.8235 0.0694 0.5322 0.2211 0.7837 0.0745 0.9884 0.5695 0.3940 0.7132 0.8586 0.2422 0.8716 0.6243 0.2036 0.7895 0.9960 0.6972 0.8296 0.3579 0.8180 0.9584 0.2545 0.8186 0.7395 0.6776 0.1017 0.5894 0.2670 0.4863 0.5726 0.4387 0.7119 0.6138 0.8064 0.9105 0.9341 0.0657 0.1424 0.6497 0.1456 0.4429 0.1756 0.2001 0.9283 0.5491 0.2555 0.8276 0.2545 0.5695 0.8464 0.8242 0.7513 0.8875 0.9189 0.5104 0.0076 0.6234 0.8732 0.8227 0.7519 0.2153 0.7688 0.0718 0.8853 0.9623 0.6011 0.5189 0.1621 0.9743 0.5574 0.8802 0.8689 0.0830 0.3505 0.9697 0.2421 0.0399 0.9654 0.3756 0.4774 0.2727 0.8888 0.3278 0.3708 0.2511 0.1115 0.0362 0.8843 0.3759 0.1242 0.5954 0.8799 0.7724 0.2923 0.2131 0.7416 0.1241 0.1979 0.7985 0.6700 0.8625 0.9413 0.8011 0.6962 0.2415 0.8749 0.1282 0.3530 0.3098 0.5582 0.7643 0.0300 0.3788 0.6612 0.8449 0.0879 0.2605 0.5342 0.8945 0.5881 0.5576 0.7421 0.6554 0.9710 0.8585 0.2157 0.9174 0.6978 0.3092 0.7490 0.7405 0.0015 0.1701 0.4483 0.4775 0.2362 0.9093 0.3844 0.7726 0.4054 0.6224 0.1453 0.7911 0.7713 0.1857 0.4590 0.1165 0.3334 0.0454 0.2124 0.3770 0.2102 0.3822 0.5553 0.0338 0.7251 0.8513 0.8557 0.1885 0.7798 0.6685 0.6982 0.7793 0.4975 0.3509 0.1764 0.2718 0.9106 0.9633 0.8904 0.4902 0.4704 0.8149 0.4929 0.1984 0.2926 0.6783 0.2912 0.4217 0.6099 0.9378 0.4330 0.6662 0.7235 0.8509 0.1286 0.8406 0.3312 0.9825 0.7004 0.4304 0.9272 0.0519 0.8986 0.2250 0.1327 0.9916 0.9887 0.7957 0.0063 0.3321 0.6965 0.9471 0.1609 0.9819 0.2010 0.8804 0.0423 0.0392
condition = ( A(26,:) - A(1,:) ) < 0.01;
A(:,condition) = A(1);
A
A = 26×500
0.9110 0.9110 0.9362 0.0277 0.9110 0.0490 0.2058 0.9110 0.9110 0.4790 0.9110 0.6323 0.7181 0.9110 0.0041 0.9110 0.1898 0.9110 0.9110 0.0844 0.9110 0.9110 0.9110 0.9110 0.3668 0.9110 0.6446 0.4606 0.7653 0.3348 0.9110 0.9110 0.3942 0.5895 0.9110 0.3949 0.5259 0.9110 0.9110 0.2583 0.9110 0.9690 0.7460 0.9110 0.3960 0.9110 0.1031 0.9110 0.9110 0.3748 0.9110 0.9110 0.9110 0.9110 0.9037 0.9110 0.9168 0.1054 0.5385 0.7585 0.9110 0.9110 0.9554 0.2019 0.9110 0.7410 0.9973 0.9110 0.9110 0.7954 0.9110 0.3300 0.6526 0.9110 0.0548 0.9110 0.5258 0.9110 0.9110 0.9178 0.9110 0.9110 0.9110 0.9110 0.3524 0.9110 0.2686 0.5587 0.9179 0.6981 0.9110 0.9110 0.5322 0.2211 0.9110 0.0745 0.9884 0.9110 0.9110 0.7132 0.9110 0.2422 0.8716 0.9110 0.2036 0.9110 0.9960 0.9110 0.9110 0.3579 0.9110 0.9110 0.9110 0.9110 0.7395 0.9110 0.1017 0.5894 0.2670 0.4863 0.9110 0.9110 0.7119 0.6138 0.9110 0.9105 0.9341 0.9110 0.9110 0.6497 0.9110 0.4429 0.1756 0.9110 0.9283 0.9110 0.2555 0.9110 0.9110 0.5695 0.9110 0.9110 0.9110 0.9110 0.9189 0.9110 0.0076 0.6234 0.8732 0.8227 0.9110 0.9110 0.7688 0.0718 0.9110 0.9623 0.6011 0.9110 0.9110 0.9743 0.9110 0.8802 0.8689 0.9110 0.3505 0.9110 0.2421 0.9110 0.9110 0.3756 0.9110 0.9110 0.9110 0.9110 0.3708 0.9110 0.1115 0.0362 0.8843 0.3759 0.9110 0.9110 0.8799 0.7724 0.9110 0.2131 0.7416 0.9110 0.9110 0.7985 0.9110 0.8625 0.9413 0.9110 0.6962 0.9110 0.8749 0.9110 0.9110 0.3098 0.9110 0.9110 0.9110 0.9110 0.6612 0.9110 0.0879 0.2605 0.5342 0.8945 0.9110 0.9110 0.7421 0.6554 0.9110 0.8585 0.2157 0.9110 0.9110 0.3092 0.9110 0.7405 0.0015 0.9110 0.4483 0.9110 0.2362 0.9110 0.9110 0.7726 0.9110 0.9110 0.9110 0.9110 0.7713 0.9110 0.4590 0.1165 0.3334 0.0454 0.9110 0.9110 0.2102 0.3822 0.9110 0.0338 0.7251 0.9110 0.9110 0.1885 0.9110 0.6685 0.6982 0.9110 0.4975 0.9110 0.1764 0.9110 0.9110 0.9633 0.9110 0.9110 0.9110 0.9110 0.4929 0.9110 0.2926 0.6783 0.2912 0.4217 0.9110 0.9110 0.4330 0.6662 0.9110 0.8509 0.1286 0.9110 0.9110 0.9825 0.9110 0.4304 0.9272 0.9110 0.8986 0.9110 0.1327 0.9110 0.9110 0.7957 0.9110 0.9110 0.9110 0.9110 0.1609 0.9110 0.2010 0.8804 0.0423 0.0392
  6 Comments
Bryce Jeffrey
Bryce Jeffrey on 18 Feb 2024
This is actually quite interesting, as part of the reason I was interested in using logical indexing was due to runtime (not actually entirely worried about performance/execution time for this project, more of a personal preference). When testing in my project as well as using the rand() in a test script, they seem to be pretty even or one just beats out the other. Potentially it has something to do with the elements created in the random array?

Sign in to comment.

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!