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

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

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

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)

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

Using for loop it can be done like this
A = rand(26,500) % zeros() just used for illustration
A = 26×500
0.1834 0.4104 0.6702 0.6751 0.2348 0.5214 0.8277 0.2371 0.2918 0.4050 0.3764 0.7522 0.9397 0.5312 0.4822 0.3717 0.9589 0.4622 0.6458 0.1530 0.0674 0.6621 0.4285 0.1007 0.5253 0.2380 0.9863 0.5802 0.4140 0.1560 0.3148 0.5546 0.9564 0.6749 0.0312 0.7109 0.3709 0.0412 0.3147 0.6773 0.9950 0.7416 0.0619 0.4877 0.6859 0.6467 0.6770 0.8711 0.5517 0.2906 0.2597 0.3575 0.4775 0.9204 0.5590 0.1271 0.3933 0.1980 0.2594 0.2660 0.0315 0.7538 0.2489 0.5661 0.2695 0.6145 0.2856 0.1520 0.9546 0.2125 0.1895 0.7216 0.7871 0.4799 0.7481 0.0110 0.1489 0.9767 0.2712 0.3279 0.1752 0.7258 0.3208 0.7392 0.9675 0.7666 0.0981 0.0555 0.1236 0.2231 0.4762 0.9791 0.0825 0.2168 0.5726 0.0476 0.7854 0.5716 0.9602 0.0391 0.0769 0.4329 0.4393 0.4617 0.0097 0.6701 0.1845 0.7476 0.6639 0.2055 0.6168 0.0515 0.9198 0.9410 0.7639 0.6094 0.4523 0.9920 0.4910 0.9140 0.9332 0.4453 0.1230 0.5854 0.8371 0.0983 0.1931 0.7302 0.4792 0.4782 0.9739 0.3434 0.3949 0.1906 0.0582 0.9875 0.3070 0.1187 0.7187 0.6968 0.0546 0.1942 0.7277 0.3369 0.9921 0.7526 0.3392 0.8025 0.0650 0.6860 0.8501 0.1169 0.1447 0.0690 0.7983 0.0996 0.5325 0.1242 0.4078 0.1358 0.2078 0.1496 0.9308 0.9989 0.5968 0.7931 0.8521 0.1380 0.1569 0.0106 0.0565 0.6651 0.5459 0.0934 0.4435 0.6184 0.3225 0.8908 0.1063 0.0693 0.8154 0.5928 0.6587 0.1614 0.3369 0.9618 0.9631 0.1636 0.8357 0.2313 0.5023 0.5925 0.8391 0.3673 0.0738 0.0342 0.4036 0.8330 0.6224 0.6709 0.9114 0.8653 0.4675 0.5783 0.7249 0.1636 0.9612 0.1804 0.5366 0.4301 0.0483 0.6089 0.2234 0.5903 0.5555 0.1848 0.7551 0.8268 0.4331 0.7028 0.6373 0.3525 0.9567 0.2366 0.5713 0.6278 0.0324 0.1164 0.5779 0.0511 0.0810 0.0352 0.8853 0.9052 0.4943 0.6072 0.7051 0.2235 0.1462 0.0689 0.5861 0.5384 0.2465 0.1221 0.2899 0.5243 0.4167 0.8464 0.2171 0.1638 0.2878 0.8574 0.0033 0.4482 0.8600 0.3442 0.1723 0.5961 0.6626 0.7090 0.9870 0.0921 0.9504 0.8166 0.3427 0.6691 0.0580 0.2648 0.4168 0.9254 0.8736 0.1194 0.2938 0.0870 0.7529 0.2306 0.0680 0.7625 0.1161 0.8928 0.0666 0.1873 0.5982 0.7061 0.8307 0.7199 0.4069 0.0138 0.7823 0.3759 0.6434 0.6364 0.0725 0.9335 0.6984 0.5015 0.3876 0.4002 0.1810 0.6373
condition = ( A(26,:) - A(1,:) ) < 0.01
condition = 1×500 logical array
Columns 1 through 49 0 0 1 1 0 1 1 0 0 1 1 1 1 0 0 0 1 0 1 0 0 0 0 0 1 0 1 1 1 0 1 0 1 1 0 0 1 0 0 1 1 0 0 1 1 0 0 1 1 Columns 50 through 98 1 0 1 0 0 0 0 1 0 1 1 0 1 1 0 1 0 1 0 0 1 0 0 1 1 0 0 0 0 1 1 1 1 0 1 0 0 1 1 1 0 0 1 0 0 0 0 1 1 Columns 99 through 147 1 1 0 0 0 0 0 0 1 0 1 1 1 1 1 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 1 0 1 1 1 1 0 1 0 1 0 0 1 1 1 Columns 148 through 196 0 1 1 0 1 0 0 1 0 1 0 1 1 1 0 1 1 0 1 1 0 0 0 1 0 0 1 0 0 0 1 1 0 1 1 1 0 0 0 0 0 1 0 1 0 0 1 1 0 Columns 197 through 245 1 1 1 1 0 0 0 1 0 0 0 0 1 0 1 1 1 1 1 0 0 1 1 0 0 1 1 1 1 1 0 1 1 1 0 0 0 1 0 0 1 0 0 0 0 0 0 1 1 Columns 246 through 294 0 0 1 1 0 1 1 0 0 1 1 0 0 1 0 1 1 0 1 1 1 1 1 0 0 0 1 1 1 0 0 1 0 0 0 1 1 0 1 0 0 1 0 0 1 1 0 1 1 Columns 295 through 343 1 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 1 1 1 0 0 1 1 0 1 0 0 0 0 1 1 0 1 1 0 1 1 0 0 0 0 1 1 Columns 344 through 392 1 1 0 0 1 0 0 0 1 1 1 0 0 0 0 0 0 0 1 1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1 1 1 1 1 0 0 Columns 393 through 441 0 0 0 1 1 0 0 1 0 1 0 1 0 1 1 1 1 1 0 1 1 1 1 1 0 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 1 1 0 0 1 1 1 1 0 Columns 442 through 490 1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 1 0 1 1 1 1 0 0 0 1 0 0 0 0 1 0 1 0 1 0 1 0 1 1 0 0 0 1 1 1 1 0 Columns 491 through 500 1 0 0 1 0 0 1 1 1 0
for k = 1:length(condition)
if condition(k) == 1
A(:,k) = A(1,k);
end
end
A
A = 26×500
0.1834 0.4104 0.6702 0.6751 0.2348 0.5214 0.8277 0.2371 0.2918 0.4050 0.3764 0.7522 0.9397 0.5312 0.4822 0.3717 0.9589 0.4622 0.6458 0.1530 0.0674 0.6621 0.4285 0.1007 0.5253 0.2380 0.9863 0.5802 0.4140 0.1560 0.3148 0.5546 0.6702 0.6751 0.0312 0.5214 0.8277 0.0412 0.3147 0.4050 0.3764 0.7522 0.9397 0.4877 0.6859 0.6467 0.9589 0.8711 0.6458 0.2906 0.2597 0.3575 0.4775 0.9204 0.5253 0.1271 0.9863 0.5802 0.4140 0.2660 0.0315 0.7538 0.6702 0.6751 0.2695 0.5214 0.8277 0.1520 0.9546 0.4050 0.3764 0.7522 0.9397 0.4799 0.7481 0.0110 0.9589 0.9767 0.6458 0.3279 0.1752 0.7258 0.3208 0.7392 0.5253 0.7666 0.9863 0.5802 0.4140 0.2231 0.4762 0.9791 0.6702 0.6751 0.5726 0.5214 0.8277 0.5716 0.9602 0.4050 0.3764 0.7522 0.9397 0.4617 0.0097 0.6701 0.9589 0.7476 0.6458 0.2055 0.6168 0.0515 0.9198 0.9410 0.5253 0.6094 0.9863 0.5802 0.4140 0.9140 0.9332 0.4453 0.6702 0.6751 0.8371 0.5214 0.8277 0.7302 0.4792 0.4050 0.3764 0.7522 0.9397 0.1906 0.0582 0.9875 0.9589 0.1187 0.6458 0.6968 0.0546 0.1942 0.7277 0.3369 0.5253 0.7526 0.9863 0.5802 0.4140 0.6860 0.8501 0.1169 0.6702 0.6751 0.7983 0.5214 0.8277 0.1242 0.4078 0.4050 0.3764 0.7522 0.9397 0.9989 0.5968 0.7931 0.9589 0.1380 0.6458 0.0106 0.0565 0.6651 0.5459 0.0934 0.5253 0.6184 0.9863 0.5802 0.4140 0.0693 0.8154 0.5928 0.6702 0.6751 0.3369 0.5214 0.8277 0.1636 0.8357 0.4050 0.3764 0.7522 0.9397 0.3673 0.0738 0.0342 0.9589 0.8330 0.6458 0.6709 0.9114 0.8653 0.4675 0.5783 0.5253 0.1636 0.9863 0.5802 0.4140 0.4301 0.0483 0.6089 0.6702 0.6751 0.5555 0.5214 0.8277 0.8268 0.4331 0.4050 0.3764 0.7522 0.9397 0.2366 0.5713 0.6278 0.9589 0.1164 0.6458 0.0511 0.0810 0.0352 0.8853 0.9052 0.5253 0.6072 0.9863 0.5802 0.4140 0.0689 0.5861 0.5384 0.6702 0.6751 0.2899 0.5214 0.8277 0.8464 0.2171 0.4050 0.3764 0.7522 0.9397 0.4482 0.8600 0.3442 0.9589 0.5961 0.6458 0.7090 0.9870 0.0921 0.9504 0.8166 0.5253 0.6691 0.9863 0.5802 0.4140 0.9254 0.8736 0.1194 0.6702 0.6751 0.7529 0.5214 0.8277 0.7625 0.1161 0.4050 0.3764 0.7522 0.9397 0.7061 0.8307 0.7199 0.9589 0.0138 0.6458 0.3759 0.6434 0.6364 0.0725 0.9335 0.5253 0.5015 0.9863 0.5802 0.4140 0.6373
Yes! The for loop is a method that I had implemented/verified that worked. The logical indexing is close, but my aim is not to replace the columns with the first element from the whole matrix, but rather the first element from that column, just like was done in the for loop method. So rather than
A(:,condition) = A(1);
I am looking for
A(:,condition) = A(1,:);
But when using the colon operator in this case, I receive an error because the size of the left side is shorter than the size of the right side (i.e. 450 on left side to 500 on right side). While I have no problem using the for loop, is the logical indexing even possible in this case?
Ok, the logical indexing is possible as demonstrated by Voss. The error is due to fitting different size for columns to multiple rows whose size is different.
tic
A = rand(26,500); % zeros() just used for illustration
condition = ( A(26,:) - A(1,:) ) < 0.01;
A(:,condition) = A(ones(end,1),condition);
A;
toc
Elapsed time is 0.007177 seconds.
tic
A = rand(26,500); % zeros() just used for illustration
condition = ( A(26,:) - A(1,:) ) < 0.01;
for k = 1:length(condition)
if condition(k) == 1
A(:,k) = A(1,k);
end
end
A;
toc
Elapsed time is 0.004075 seconds.
I dont understand why the logical indexing using arrays is slower compared to loop constructs. It used to be or designed to execute faster than loop construct by MATLAB.
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?
May be its designed to eliminate the extra lines of coding

Sign in to comment.

Products

Release

R2023a

Asked:

on 18 Feb 2024

Commented:

on 18 Feb 2024

Community Treasure Hunt

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

Start Hunting!