i have a column array and i want to subtract each row by the first row and save.please help me in doing this

38 views (last 30 days)
i have a column array and i want to subtract each row by the first row and save.please help me in doing this

Accepted Answer

Star Strider
Star Strider on 7 Dec 2017
Try this:
Array = randi(9, 5, 4); % Create Data
Out = Array - Array(1,:); % Subtract First Row From All Rows
  4 Comments
Kurni Eswar
Kurni Eswar on 9 Dec 2017
I have one more problem. Please help me I want to remove 2 rows above and below, when the difference between two rows in first column is more than 2. For example: if have an array a=[0 2 2; 2 5 3; 4 2 6; 10 6 7; 12 5 9; 14 2 7; 15 5 6] i want a new array of
a= [0 2 2; 15 5 6]
Star Strider
Star Strider on 9 Dec 2017
This is obviously homework, so I am reluctant to give a complete solution. I will describe the approach instead.
You can find the indices of the rows matching those criteria with:
idx = find(diff([0; a(:,1)])>2);
(note that it is necessary to add a 0 at the beginning of the vector so the indices are correct), then choose all the rows defined by idx ±2 and delete them by setting them equal to the empty array, [].
That worked when I coded it.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!