You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MATLAB中定位单元格数组共享相同字符串的行及数据结构化需求

Got it, let's tackle this MATLAB problem step by step! I'll walk you through a practical, scalable script to convert your cell array into the required row-per-node-and-generation-type structure.

MATLAB Cell Array to Structured Row Data Solution

First, let's start with a simplified example of your raw cell array (matching the multi-plant, node, generation type scenario you mentioned):

% Raw cell array: columns = Plant Name, Node ID, Generation Type, Power Output
rawData = {
    'Plant A', 'Node1', 'Solar', 120;
    'Plant A', 'Node1', 'Wind', 80;
    'Plant B', 'Node2', 'Solar', 90;
    'Plant B', 'Node3', 'Wind', 150;
    'Plant C', 'Node2', 'Hydro', 200;
};

Here's how to automate the conversion to a "one row per node + generation type" structure:

Step 1: Extract Unique Nodes and Generation Types

First, we need to capture every unique node and generation type present in the data to ensure full coverage:

% Get unique nodes from the 2nd column
uniqueNodes = unique(rawData(:,2));
% Get unique generation types from the 3rd column
uniqueGenTypes = unique(rawData(:,3));

Step 2: Generate All Node-Generation Type Combinations

This is the core step to create every possible pairing. We'll use meshgrid to generate the full cartesian product:

% Create grid of all combinations
[nodeGrid, typeGrid] = meshgrid(uniqueNodes, uniqueGenTypes);
% Flatten grids into column vectors for easy concatenation
allNodes = nodeGrid(:);
allGenTypes = typeGrid(:);

Step 3: Match Power Outputs from Raw Data

Now we'll map each combination to its corresponding power output. For pairs that don't exist in the raw data, we'll default to 0 (you can swap this for NaN if needed):

% Initialize output array with zeros
powerOutput = zeros(size(allNodes));

% Loop through each combination to find matches
for i = 1:length(allNodes)
    % Find rows where both node and generation type match
    matchMask = strcmp(rawData(:,2), allNodes(i)) & strcmp(rawData(:,3), allGenTypes(i));
    if any(matchMask)
        % Assign the power output (sum if multiple entries exist for the same pair)
        powerOutput(i) = sum(cell2mat(rawData(matchMask, 4)));
    end
end

Step 4: Build the Final Structured Output

You can choose between a MATLAB struct or table (tables are more user-friendly for data inspection and manipulation):

% Option 1: Create a structure array
resultStruct = struct(...
    'NodeID', allNodes, ...
    'GenerationType', allGenTypes, ...
    'PowerOutput', num2cell(powerOutput));

% Option 2: Create a table (recommended for readability)
resultTable = table(allNodes, allGenTypes, powerOutput, ...
    'VariableNames', {'NodeID', 'GenerationType', 'PowerOutput'});

After running this, resultTable will have a row for every node-generation type pair—even pairs like Node1-Hydro that weren't in the raw data (with a 0 power output).

Performance Optimization for Large Datasets

If you're working with huge datasets, replace the loop with a vectorized approach using ismember for faster execution:

% Use row-wise membership to find matches
[~, matchIdx] = ismember([allNodes, allGenTypes], [rawData(:,2), rawData(:,3)], 'rows');
% Assign power outputs, default to 0 for missing pairs
powerOutput = zeros(size(allNodes));
powerOutput(matchIdx ~= 0) = cell2mat(rawData(matchIdx(matchIdx ~= 0), 4));

内容的提问来源于stack exchange,提问作者Zed Kay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:27:23