比较两列并在Matlab中求和

时间:2018-07-04 12:52:26

标签: arrays matlab

我有2列这样的内容:

0.0    1.2
0.0    2.3
0.0    1.5
0.1    1.0
0.1    1.2
0.1    1.4
0.1    1.7
0.4    1.1
0.4    1.3
0.4    1.5

在第一列中,将0.0重复3次。我想对相应的元素求和 (1.2 + 2.3 + 1.5)在第二列。同样,第1次重复0.1次 柱。我想在第二个中求和对应的元素(1.0 + 1.2 + 1.4 + 1.7) 列等等。

我正在尝试这样

for i = 1:length(col1)
    for j = 1:length(col2)
        % if col2(j) == col1(i)
        % to do
        end
    end
end

2 个答案:

答案 0 :(得分:5)

这是uniqueaccumarray的经典用法:

accumarray

您也可以使用较新的splitapply函数代替[~, ~, w] = unique(x(:,1)); % labels of unique elements result = splitapply(@sum, x(:,2), w); % sum using the above as grouping variable

{
    "responseId": "552fd979-bf0f-4f6c-9ddd-39180824c26d",
    "queryResult": {
            "queryText": "GOOGLE_ASSISTANT_WELCOME",
            "parameters": {},
            "allRequiredParamsPresent": true,
            "fulfillmentMessages": [
                    {
                            "text": {
                                    "text": [
                                            ""
                                    ]
                            }
                    }
            ],
            "outputContexts": [
                    {
                            "name": "projects/trythis-807dd/agent/sessions/1530712293227/contexts/google_assistant_welcome"
                    },
                    {
                            "name": "projects/trythis-807dd/agent/sessions/1530712293227/contexts/actions_capability_screen_output"
                    },
                    {
                            "name": "projects/trythis-807dd/agent/sessions/1530712293227/contexts/actions_capability_audio_output"
                    },
                    {
                            "name": "projects/trythis-807dd/agent/sessions/1530712293227/contexts/google_assistant_input_type_keyboard"
                    },
                    {



{
    "fulfillmentText": "<speak>Hello! What's your name?</speak>",
    "payload": {
            "google": {
                    "expectUserResponse": true,
                    "richResponse": {
                            "items": [
                                    {
                                            "simpleResponse": {
                                                    "ssml": "<speak>Hello! What's your name?</speak>"
                                            }
                                    }
                            ]
                    },
                    "noInputPrompts": [
                            {
                                    "ssml": "<speak>Please tell me your name.</speak>"
                            }
                    ]
            }
    },

{
    "responseId": "8d031a0f-9692-4e8d-a172-d7dad9c7511b",
    "queryResult": {
            "queryText": "my name is salma",
            "parameters": {
                    "name": "Salma"
            },
            "allRequiredParamsPresent": true,
            "fulfillmentMessages": [
                    {
                            "text": {
                                    "text": [
                                            ""
                                    ]
                            }
                    }
            ],
            "outputContexts": [
                    {
                            "name": "projects/trythis-807dd/agent/sessions/1530712293227/contexts/actions_capability_screen_output",
                            "parameters": {
                                    "name.original": "salma",
                                    "name": "Salma"
                            }
                    },


{
    "fulfillmentText": "<speak>Hello  Salma, nice to meet you! what do you want me to do for you?</speak>",
    "payload": {
            "google": {
                    "expectUserResponse": true,
                    "richResponse": {
                            "items": [
                                    {
                                            "simpleResponse": {
                                                    "ssml": "<speak>Hello  Salma, nice to meet you! what do you want me to do for you?</speak>"
                                            }
                                    }
                            ]
                    },
                    "noInputPrompts": [
                            {
                                    "ssml": "<speak>Hey , Salma,  What can i do ?</speak>"
                            }
                    ]
            }
    },


{
    "fulfillmentText": "<speak>Hello  Salma, nice to meet you! what do you want me to do for you?</speak>",
    "payload": {
            "google": {
                    "expectUserResponse": true,
                    "richResponse": {
                            "items": [
                                    {
                                            "simpleResponse": {
                                                    "ssml": "<speak>Hello  Salma, nice to meet you! what do you want me to do for you?</speak>"
                                            }
                                    }
                            ]
                    },
                    "noInputPrompts": [
                            {
                                    "ssml": "<speak>Hey , Salma,  What can i do ?</speak>"
                            }
                    ]
            }
    },


 "fulfillmentText": "<speak>Do you want me to play the radio called A Better Radio ?</speak>",

答案 1 :(得分:1)

  constructor( private http: HttpClient ) {}

  const headers = new HttpHeaders()
     .set('Content-Type', 'application/json')
     .set('Accept', 'application/json')

  const url = 'http://localhost:5000/api/'  

  postData( params ) {
     return this.http.post(
        url + 'grupos',
        JSON.stringify(params),
        { headers: headers }
     );        
  }

礼物:

a=[0.0    1.2
0.0    2.3
0.0    1.5
0.1    1.0
0.1    1.2
0.1    1.4
0.1    1.7
0.4    1.1
0.4    1.3
0.4    1.5]
% Get unique col1 values, and indices
[uniq,~,ib]=unique(a(:,1));

% for each unique value in col1
for ii=1:length(uniq)
    % sum all col2 values that correspond to the current index of the unique value
    s(ii)=sum(a(ib==ii,2));
end