Neel Kulkarni on 23 Jun 2024 at 13:19
-
-
Link
Direct link to this question
https://www.mathworks.com/matlabcentral/answers/2131196-subtract-column-values-where-leading-columns-have-values-flipped-i-e-a-b-c1-b-a-c2
Edited: dpb on 24 Jun 2024 at 15:56
Suppose I have a table, T:
A B C
1610
2520
3430
4345
526
6110
I want to subtract each entry in column C where the A and B values are flipped, and put this value into column D
For example, the first row (1, 6, 10) - the last row (6, 1, 10); (2, 5, 20) - (5, 2, 6) and so on
So D should be:
14
-15
15
-14
Thanks a lot!
Edit: (i) Flipped rows could occur anywhere in the table; (ii) there may be repeated values in Α and B
10 Comments Show 8 older commentsHide 8 older comments
Show 8 older commentsHide 8 older comments
John D'Errico on 23 Jun 2024 at 13:38
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2131196-subtract-column-values-where-leading-columns-have-values-flipped-i-e-a-b-c1-b-a-c2#comment_3193686
Edited: John D'Errico on 23 Jun 2024 at 13:39
Define "flipped". The exmple you show does not suggest what it might mean.
Torsten on 23 Jun 2024 at 13:46
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2131196-subtract-column-values-where-leading-columns-have-values-flipped-i-e-a-b-c1-b-a-c2#comment_3193691
⋮
Open in MATLAB Online
M = [1610
2520
3430
4345
526
6110];
D = M(:,3)-flipud(M(:,3))
D = 6x1
0 14 -15 15 -14 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Neel Kulkarni on 23 Jun 2024 at 13:52
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2131196-subtract-column-values-where-leading-columns-have-values-flipped-i-e-a-b-c1-b-a-c2#comment_3193701
The first row has (A, B, C) as (1, 6, 10). The last row has the first two values flipped, i.e., (6, 1, 10). Similarly between the second and penultimate row the first two values are flipped: 2, 5, 20 and 5, 2, 6. I would like the code to identify such rows and subtract the C column values from each other (so 10-10 = 0; 20-6 = 14)
Image Analyst on 23 Jun 2024 at 13:55
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2131196-subtract-column-values-where-leading-columns-have-values-flipped-i-e-a-b-c1-b-a-c2#comment_3193706
Is the flipped row always at numRows-row+1, like 1 and 6, 2 and 5, 3 and 4, or might the "flipped row" occur anywhere, at any row?
Torsten on 23 Jun 2024 at 14:01
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2131196-subtract-column-values-where-leading-columns-have-values-flipped-i-e-a-b-c1-b-a-c2#comment_3193711
@Image Analyst
Seems it can occur anywhere - otherwise my suggested solution would work.
Neel Kulkarni on 23 Jun 2024 at 14:05
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2131196-subtract-column-values-where-leading-columns-have-values-flipped-i-e-a-b-c1-b-a-c2#comment_3193726
Edited: Neel Kulkarni on 23 Jun 2024 at 14:07
@Torsten thanks for your solution; indeed it works if the example is stuck to, but my actual, larger dataset is not as orderly unfortunately. The flipped rows occur anywhere, and there are repeated values in A and B columns as well (forgot to add this in the original question, have added it as an edit just now). The (A,B) couple itself is flipped, not merely that B is A flipped upside down. Apologies that the original question was not clear about this.
Torsten on 23 Jun 2024 at 14:12
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2131196-subtract-column-values-where-leading-columns-have-values-flipped-i-e-a-b-c1-b-a-c2#comment_3193741
Please give an example for
there are repeated values in A and B columns as well
and how this case is handled.
Neel Kulkarni on 23 Jun 2024 at 14:25
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2131196-subtract-column-values-where-leading-columns-have-values-flipped-i-e-a-b-c1-b-a-c2#comment_3193751
Edited: Neel Kulkarni on 23 Jun 2024 at 14:28
@Torsten
A B C D
164 -6
269 3
2 5 10 -3
3610 No flipped row (6,3) so preserve C value (=10)
4345 No flipped row (3,4), preserve C value (=45)
5 2 13 3
626 -3
6110 6
Image Analyst on 23 Jun 2024 at 14:49
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2131196-subtract-column-values-where-leading-columns-have-values-flipped-i-e-a-b-c1-b-a-c2#comment_3193761
Open in MATLAB Online
Do you have a table variable, or a double variable? Can you attach your variable in a mat file?
save('answers.mat', 'T');
When you say "flipped" do you mean vertically or horizontally? Your original example seemed to have it both ways. Now it seems just that you mean horizontally, so that columnA is in columnB and ColumnB is in columnA, so in essence for a particular row, colA and colB are swapped.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
TUTORIAL: How to ask a question (on Answers) and get a fast answer
Neel Kulkarni on 23 Jun 2024 at 15:20
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2131196-subtract-column-values-where-leading-columns-have-values-flipped-i-e-a-b-c1-b-a-c2#comment_3193776
- answers.mat
@Image Analyst Hi, it is a table variable. I have attached the mat file with my example above. It indeed is a horizontal flip.
Sign in to comment.