Subtract column values where leading columns have values flipped (i... (2024)

76 visualizzazioni (ultimi 30 giorni)

Mostra commenti meno recenti

Neel Kulkarni il 23 Giu 2024 alle 13:19

  • Link

    Link diretto a questa domanda

    https://it.mathworks.com/matlabcentral/answers/2131196-subtract-column-values-where-leading-columns-have-values-flipped-i-e-a-b-c1-b-a-c2

  • Link

    Link diretto a questa domanda

    https://it.mathworks.com/matlabcentral/answers/2131196-subtract-column-values-where-leading-columns-have-values-flipped-i-e-a-b-c1-b-a-c2

Modificato: dpb il 24 Giu 2024 alle 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 Commenti

Mostra 8 commenti meno recentiNascondi 8 commenti meno recenti

John D'Errico il 23 Giu 2024 alle 13:38

Link diretto a questo commento

https://it.mathworks.com/matlabcentral/answers/2131196-subtract-column-values-where-leading-columns-have-values-flipped-i-e-a-b-c1-b-a-c2#comment_3193686

  • Link

    Link diretto a questo commento

    https://it.mathworks.com/matlabcentral/answers/2131196-subtract-column-values-where-leading-columns-have-values-flipped-i-e-a-b-c1-b-a-c2#comment_3193686

Modificato: John D'Errico il 23 Giu 2024 alle 13:39

Define "flipped". The exmple you show does not suggest what it might mean.

Torsten il 23 Giu 2024 alle 13:46

Link diretto a questo commento

https://it.mathworks.com/matlabcentral/answers/2131196-subtract-column-values-where-leading-columns-have-values-flipped-i-e-a-b-c1-b-a-c2#comment_3193691

  • Link

    Link diretto a questo commento

    https://it.mathworks.com/matlabcentral/answers/2131196-subtract-column-values-where-leading-columns-have-values-flipped-i-e-a-b-c1-b-a-c2#comment_3193691

Neel Kulkarni il 23 Giu 2024 alle 13:52

Link diretto a questo commento

https://it.mathworks.com/matlabcentral/answers/2131196-subtract-column-values-where-leading-columns-have-values-flipped-i-e-a-b-c1-b-a-c2#comment_3193701

  • Link

    Link diretto a questo commento

    https://it.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 il 23 Giu 2024 alle 13:55

Link diretto a questo commento

https://it.mathworks.com/matlabcentral/answers/2131196-subtract-column-values-where-leading-columns-have-values-flipped-i-e-a-b-c1-b-a-c2#comment_3193706

  • Link

    Link diretto a questo commento

    https://it.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 il 23 Giu 2024 alle 14:01

Link diretto a questo commento

https://it.mathworks.com/matlabcentral/answers/2131196-subtract-column-values-where-leading-columns-have-values-flipped-i-e-a-b-c1-b-a-c2#comment_3193711

  • Link

    Link diretto a questo commento

    https://it.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 il 23 Giu 2024 alle 14:05

Link diretto a questo commento

https://it.mathworks.com/matlabcentral/answers/2131196-subtract-column-values-where-leading-columns-have-values-flipped-i-e-a-b-c1-b-a-c2#comment_3193726

  • Link

    Link diretto a questo commento

    https://it.mathworks.com/matlabcentral/answers/2131196-subtract-column-values-where-leading-columns-have-values-flipped-i-e-a-b-c1-b-a-c2#comment_3193726

Modificato: Neel Kulkarni il 23 Giu 2024 alle 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 il 23 Giu 2024 alle 14:12

Link diretto a questo commento

https://it.mathworks.com/matlabcentral/answers/2131196-subtract-column-values-where-leading-columns-have-values-flipped-i-e-a-b-c1-b-a-c2#comment_3193741

  • Link

    Link diretto a questo commento

    https://it.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 il 23 Giu 2024 alle 14:25

Link diretto a questo commento

https://it.mathworks.com/matlabcentral/answers/2131196-subtract-column-values-where-leading-columns-have-values-flipped-i-e-a-b-c1-b-a-c2#comment_3193751

  • Link

    Link diretto a questo commento

    https://it.mathworks.com/matlabcentral/answers/2131196-subtract-column-values-where-leading-columns-have-values-flipped-i-e-a-b-c1-b-a-c2#comment_3193751

Modificato: Neel Kulkarni il 23 Giu 2024 alle 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 il 23 Giu 2024 alle 14:49

Link diretto a questo commento

https://it.mathworks.com/matlabcentral/answers/2131196-subtract-column-values-where-leading-columns-have-values-flipped-i-e-a-b-c1-b-a-c2#comment_3193761

  • Link

    Link diretto a questo commento

    https://it.mathworks.com/matlabcentral/answers/2131196-subtract-column-values-where-leading-columns-have-values-flipped-i-e-a-b-c1-b-a-c2#comment_3193761

Apri 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 il 23 Giu 2024 alle 15:20

Link diretto a questo commento

https://it.mathworks.com/matlabcentral/answers/2131196-subtract-column-values-where-leading-columns-have-values-flipped-i-e-a-b-c1-b-a-c2#comment_3193776

  • Link

    Link diretto a questo commento

    https://it.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.

Accedi per commentare.

Accedi per rispondere a questa domanda.

Risposte (1)

dpb il 23 Giu 2024 alle 16:35

  • Link

    Link diretto a questa risposta

    https://it.mathworks.com/matlabcentral/answers/2131196-subtract-column-values-where-leading-columns-have-values-flipped-i-e-a-b-c1-b-a-c2#answer_1475951

  • Link

    Link diretto a questa risposta

    https://it.mathworks.com/matlabcentral/answers/2131196-subtract-column-values-where-leading-columns-have-values-flipped-i-e-a-b-c1-b-a-c2#answer_1475951

Modificato: dpb il 24 Giu 2024 alle 15:56

Apri in MATLAB Online

  • answers.mat

load answers T

T

T = 8x3 table

A B C _ _ __ 1 6 4 2 6 9 2 5 10 3 6 10 4 3 45 5 2 13 6 2 6 6 1 10

T.D=nan(height(T),1);

for i=1:height(T)

ix=find(all(fliplr(T{:,1:2})==T{i,1:2},2));

try

T.D(i)=T.C(i)-T.C(ix);

catch

end

end

T

T = 8x4 table

A B C D _ _ __ ___ 1 6 4 -6 2 6 9 3 2 5 10 -3 3 6 10 NaN 4 3 45 NaN 5 2 13 3 6 2 6 -3 6 1 10 6

The above is the "dead ahead" solution that computes the difference for every record in the file; one could be a little more clever and set both i and ix (to the negative difference) in the one loop iteration. Then one would need to make and update the list of records yet to be handled to know when to quit. The list would start initialized to 1:height(T) and each i, ix would be removed each iteration. When empty, quit.

The above does nothing when there is no match, thereby leaving the initial default NaN value; your choice as to what to do here if this isn't the desired result. Your initial example had no unpaired records and your followup didn't define what is the desired result for those records. In this case, the catch clause would have to remove those i entries. Whether shortening the number of iterations would "win" over the extra logic probably would depend upon how large T is.

0 Commenti

Mostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Accedi per rispondere a questa domanda.

Vedere anche

Categorie

MATLABGraphicsFormatting and AnnotationLabels and AnnotationsAnnotations

Scopri di più su Annotations in Help Center e File Exchange

Tag

  • subtraction
  • tables values

Prodotti

  • MATLAB

Release

R2024a

Community Treasure Hunt

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

Start Hunting!

Si è verificato un errore

Impossibile completare l'azione a causa delle modifiche apportate alla pagina. Ricarica la pagina per vedere lo stato aggiornato.


Translated by Subtract column values where leading columns have values flipped (i... (13)

Subtract column values where leading columns have values flipped (i... (14)

Seleziona un sito web

Seleziona un sito web per visualizzare contenuto tradotto dove disponibile e vedere eventi e offerte locali. In base alla tua area geografica, ti consigliamo di selezionare: .

Puoi anche selezionare un sito web dal seguente elenco:

Americhe

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europa

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia-Pacifico

Contatta l’ufficio locale

Subtract column values where leading columns have values flipped (i... (2024)

References

Top Articles
Latest Posts
Article information

Author: Rev. Porsche Oberbrunner

Last Updated:

Views: 6040

Rating: 4.2 / 5 (53 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Rev. Porsche Oberbrunner

Birthday: 1994-06-25

Address: Suite 153 582 Lubowitz Walks, Port Alfredoborough, IN 72879-2838

Phone: +128413562823324

Job: IT Strategist

Hobby: Video gaming, Basketball, Web surfing, Book restoration, Jogging, Shooting, Fishing

Introduction: My name is Rev. Porsche Oberbrunner, I am a zany, graceful, talented, witty, determined, shiny, enchanting person who loves writing and wants to share my knowledge and understanding with you.