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?
채택된 답변
추가 답변 (1개)
A = rand(26,500) % zeros() just used for illustration
condition = ( A(26,:) - A(1,:) ) < 0.01;
A(:,condition) = A(1);
A
댓글 수: 6
Using for loop it can be done like this
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
Bryce Jeffrey
2024년 2월 18일
VBBV
2024년 2월 18일
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
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
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.
Bryce Jeffrey
2024년 2월 18일
VBBV
2024년 2월 18일
May be its designed to eliminate the extra lines of coding
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!