Use conditions on a matrix and manipulate the results

2 次查看(过去 30 天)
Hello,
I have a matrix:
a = [1 1 1 1; 0.99 1 1 1; 0.98 0.99 1 1; 0.97 0.99 0.99 0.99]
I would like to obtain the following result:
b = [0 0 0 0; 2 0 0 0; 0 3 0 0; 0 0 4 4]
It means that at the first time when 1 changes to 0.99 look up which row is it and write it in the 'b' matrix at the same place. If this condition is not true, write zero.
Thank you!

采纳的回答

Star Strider
Star Strider 2014-9-29
This works:
a = [1 1 1 1; 0.99 1 1 1; 0.98 0.99 1 1; 0.97 0.99 0.99 0.99];
q1 = a(1:end-1,:) - a(2:end,:); % Subtract Rows From Offset Rows
q2 = a(2:end,:)==0.99; % Find Offset Rows = 0.99
q3 = [zeros(1,size(a,2)); q1 & q2]; % Create Logical Matrix (Cond 1 & Cond 2)
b = bsxfun(@times, q3, [1:size(a,1)]'); % Use ‘bsxfun’ To Number The Row Entries

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by