Info

This question is closed. Reopen it to edit or answer.

Sums elements of a row

4 views (last 30 days)
Riccardo Busin
Riccardo Busin on 22 Feb 2020
Closed: MATLAB Answer Bot on 20 Aug 2021
Hello everyone!
I have the following problem (I bring an example):
I am in this initial situation:
Company worker1 worker2 worker3 worker4 worker5
AAAAA M F F F M
BBBBB F F M
CCCC M M F M
DDDDD F M
I would like to get the sum of M and F for each line and have this as the final result:
Company worker1 worker2 worker3 worker4 worker5 SumM SumF
AAAAA M F F F M 2 3
BBBBB F F M 1 2
CCCC M M F M 3 1
DDDDD F F 0 2
Finally, save it all in excel
I attach a piece of code that I wrote (I don't know if it is right or useful to achieve the required result)
clear all;
clc;
load('DBTot.mat');
%%
n=size(DBTot,1);
m=size(DBTot,2);
DBGender=DBTot(:,[1,(7:4:m)]); % selec only the gender
DBGender=table2cell(DBGender);
n1=size(DBGender,1);
m1=size(DBGender,1);
clear DBTot
strDBGender=string(DBGender);
Thank you all!
  3 Comments
Riccardo Busin
Riccardo Busin on 22 Feb 2020
Thank you for your help!
I changed the example (I noticed that the companies I am examining have a different number of workers). In this new case, does the code you wrote remain the same?
Thanks again!
Walter Roberson
Walter Roberson on 23 Feb 2020
DBGender = DBTot{:,7:4:m};
Looks more plausible to me. It would be odd to have gender as the first column.
By the way, you should be improving the logic to count all the other genders as well as M and F.

Answers (0)

This question is closed.

Tags

Community Treasure Hunt

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

Start Hunting!