C++改写Matlab源码实践二之【传递表格数据】————附带详细讲解和示例

2023-05-16

文章目录

  • 0 背景
  • 1 Matlab有用代码提取
  • 2 转换为C++
    • 2.1 设定变量类型
    • 2.2 预先和定义声明变量
  • 3 改写转换后的C++代码
    • 3.1 指定输入的文件
    • 3.2 改写处理`Data`和`Data1`结构的数据
  • 4 福利

0 背景

本文是Matlab代码转C++的第二篇,目的是把一个更为复杂的Matlab项目代码转换为C++,以便打包成动态链接库,以供Java或C++调用。转换的难点,在于需要大量改写转换后的代码,把固定的输入数据,转成可以指定文件的方法,并把对数据结构做适当的处理以便调用放获取数据。

以下是文件的目录结构,untitled.m调用了从上往下的前三个.m文件,并且untitled.m文件中的内容比较多,改写起来也不容易。
在这里插入图片描述

matlab运行后的结果(我们需要做的就是把matlab代码转成C++,然后使用其他语言调用C++代码,可以得到输出表格中的数据):

在这里插入图片描述

1 Matlab有用代码提取

以下为Matlab项目入口的untitled.m的部分原代码:

function varargout = untitled(varargin)
% UNTITLED MATLAB code for untitled.fig
%      UNTITLED, by itself, creates a new UNTITLED or raises the existing
%      singleton*.
%
%      H = UNTITLED returns the handle to a new UNTITLED or the handle to
%      the existing singleton*.
%
%      UNTITLED('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in UNTITLED.M with the given input arguments.
%
%      UNTITLED('Property','Value',...) creates a new UNTITLED or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before untitled_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to untitled_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help untitled

% Last Modified by GUIDE v2.5 25-May-2022 11:48:46

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @untitled_OpeningFcn, ...
                   'gui_OutputFcn',  @untitled_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before untitled is made visible.
function untitled_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to untitled (see VARARGIN)
set(handles.Dimension,'String','');
set(handles.Numbers,'String','');
set(handles.Clusters,'String','');
set(handles.Iteration,'String','');
% Choose default command line output for untitled
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes untitled wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Executes on button press in Load.
function Load_Callback(hObject, eventdata, handles)
% hObject    handle to Load (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% 数据载入控制
global data;
global data1;
[f,p]=uigetfile('*.mat');
filepath=[p,f]; %filepath即为文件路径
load(filepath); 
data = Data;
data1 = Data1;
set(handles.Dimension,'String',num2str(size(Data,2)));
set(handles.Numbers,'String',num2str(size(Data,1)));

% --- Executes on button press in Reset.
function Reset_Callback(hObject, eventdata, handles)
% hObject    handle to Reset (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.Dimension,'String','');
set(handles.Numbers,'String','');
set(handles.Clusters,'String','');
set(handles.Iteration,'String','');

% --- Executes on button press in Perform.
function Perform_Callback(hObject, eventdata, handles)
% hObject    handle to Perform (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global data;
global data1;
Cope = get(handles.Iteration,'String');
   if isempty(Cope)
    pause(0.25);
    h=msgbox('请先填写最大迭代次数(50-500)');
    uiwait(h,5);
   else
[~,~,Phi] = PROMETHEE(data);
% clf;
% axes();
bar(handles.Net,Phi)
box off
% set(gca,'XTick',[20 40 60 80 100 120 140 160])
set(get(gca,'YLabel'),'String','The Net Outranking Flow','FontSize',16,'FontWeight','demi','LineWidth',1.5,'FontName','Times New Roman');
T=str2num(get(handles.Iteration,'String'));
[Ss,Uu,V,objFcn,ddmin,times,iter] = myfcm(Phi', T);
% figure(2)
% clf;
bar(handles.ObjFunction,Ss)
box off
set(gca,'Xtick',[1 2 3 4 5 6 7 8 9],'XTicklabel',num2str([2 3 4 5 6 7 8 9 10]'))
ylabel('Objective Function','FontSize',16,'FontWeight','demi','LineWidth',1.5,'FontName','Times New Roman')
xlabel('Number of Clusters','FontSize',16,'FontWeight','demi','LineWidth',1.5,'FontName','Times New Roman')
[~,OptClus]=min(Ss);
set(handles.Clusters,'String',num2str(OptClus+1));
membership = Uu{2};
for i=1:size(data,1)
    [~,Label(i)]=max(membership(:,i));
end
% res1 = cell(length(find(Label==3)),1);
% res2 = cell(length(find(Label==2)),1);
% res3 = cell(length(find(Label==1)),1);
% res4 = cell(length(find(Label==4)),1);
% res5 = cell(length(find(Label==5)),1);
% OFva1 = cell(length(find(Label==3)),1);
% OFva2 = cell(length(find(Label==2)),1);
% OFva3 = cell(length(find(Label==1)),1);
% OFva4 = cell(length(find(Label==4)),1);
% OFva5 = cell(length(find(Label==5)),1);
% OFva
Resnet = cell(1,3);
Resnet{1} = Phi(find(Label==1));
Resnet{2} = Phi(find(Label==2));
Resnet{3} = Phi(find(Label==3));

meanOF = [mean(Resnet{1}) mean(Resnet{2}) mean(Resnet{3})];
[~,iii] = sort(meanOF,'descend');
[~,c]=sort(Resnet{iii(1)},'descend');
indexc = find(Label==iii(1));
indexc = indexc(c);
[~,c]=sort(Resnet{iii(3)},'descend');
indexc3 = find(Label==iii(3));
indexc3 = indexc3(c);
[~,c]=sort(Resnet{iii(2)},'descend');
indexc2 = find(Label==iii(2));
indexc2 = indexc2(c);
% OFval1=Phi(indexc);
% OFval3=Phi(indexc3);
% OFval2=Phi(indexc2);

Result1 = cell(size(data,1),1);
Result1{1} = 'rank 1';
for i=2:length(indexc)+1
    Result1{i}=data1{indexc(i-1)};
end
% Num = size(data,1);
% xlswrite('Res1.xls',res1,2,'A1');
% xlswrite('Res1.xls',OFval1,1,['A',num2str(length(res11)+1)]);

Result1{1+length(indexc)} = 'rank 2';
for i=2:length(indexc2)+1
    Result1{i+length(indexc)}=data1{indexc2(i-1)};
%     OFval2{i}=Phi(indexc2(i));
end
% xlswrite('Res1.xls',res2,1,'C1');
% xlswrite('Res1.xls',OFval2,2,['A',num2str(length(res22)+1)]);
Result1{1+length(indexc)+length(indexc2)} = 'rank 3';
for i=2:length(indexc3)+1
    Result1{i+length(indexc)+length(indexc2)}=data1{indexc3(i-1)};
%     OFval3{i}=Phi(indexc3(i));
end
% for i=1:length(res22)+length(res33)+length(res11)
%     Result1{i}
% end
% xlswrite('Res1.xls',Result1);
% Res1=xlsread('Res1.xls');%读取文件datafile.xlsx,并存入data中
set(handles.ResultTable,'data',Result1);%将data中的文件以Data的形式设置在句柄为uitable1的表格中。
   end
% xlswrite('Res1.xls',OFval3,3,['A',num2str(length(res33)+1)]);


% 净值

下面是转换后的代码(找到原代码中输出的部分,然后做适当修改后,改写成方法,该方法可以调用其他方法):

function [Result1, Clusters, Phi, Ss] = Perform(data,data1,T)
    [~,~,Phi] = PROMETHEE(data);
    [Ss,Uu,~,~,~,~] = myfcm(Phi', T);
    [~,OptClus] = min(Ss);
    Clusters = OptClus+1;
    membership = Uu{2};

    dataSize=size(data,1);
    Label=coder.nullcopy(zeros(1,dataSize));
    for i=1:dataSize
        [~,Label(i)]=max(membership(:,i));
    end
    Resnet = cell(1,3);
    Resnet{1} = Phi(Label==1);
    Resnet{2} = Phi(Label==2);
    Resnet{3} = Phi(Label==3);

    meanOF = [mean(Resnet{1}) mean(Resnet{2}) mean(Resnet{3})];
    [~,iii] = sort(meanOF,'descend');
    [~,c]=sort(Resnet{iii(1)},'descend');
    indexc = find(Label==iii(1));
    indexc = indexc(c);
    [~,c]=sort(Resnet{iii(3)},'descend');
    indexc3 = find(Label==iii(3));
    indexc3 = indexc3(c);
    [~,c]=sort(Resnet{iii(2)},'descend');
    indexc2 = find(Label==iii(2));
    indexc2 = indexc2(c);

    Result1 = cell(dataSize,1);
    for i=1:dataSize
        Result1{i}='000';
    end

    Result1{1} = 'rank 1';
    
    
    for i=2:length(indexc)+1
        Result1{i}=data1{indexc(i-1)};
    end

    Result1{1+length(indexc)} = 'rank 2';
    for i=2:length(indexc2)+1
        Result1{i+length(indexc)}=data1{indexc2(i-1)};
    end
    Result1{1+length(indexc)+length(indexc2)} = 'rank 3';
    for i=2:length(indexc3)+1
        Result1{i+length(indexc)+length(indexc2)}=data1{indexc3(i-1)};
    end
end

2 转换为C++

参考之前的方法,做C++代码转换,需要注意的时,在设定变量类型时,需要做适当修改,不能使用原始的自动化生成的变量。

2.1 设定变量类型

如果使用了自动检测的,就会爆出如下的错误:

在这里插入图片描述
这是改写前的数据类型:

在这里插入图片描述
这是改写后的数据类型:
在这里插入图片描述

2.2 预先和定义声明变量

这是原始版本:

indexc = find(Label==iii(1));
indexc = indexc(c);

Result1{1} = 'rank 1';
for i=2:length(indexc)+1
    Result1{i}=data1{indexc(i-1)};
end

这是改写后的版本:

Result1 = cell(dataSize,1);
dataSize=size(data,1);
    
for i=1:dataSize
   Result1{i}='000';
end

Result1{1} = 'rank 1';
for i=2:length(indexc)+1
   Result1{i}=data1{indexc(i-1)};
end

3 改写转换后的C++代码

转换出来的C++代码的示例代码并不能直接使用,我们需要对其进行修改。

下面是转换后的示例代码example/main.cpp(只有固定的输入【定死了的.mat文件】、输出):

static void main_Perform()
{
  coder::array<cell_wrap_0, 2U> data1;
  coder::array<cell_wrap_0, 1U> Result1;
  coder::array<double, 2U> Phi;
  coder::array<double, 2U> data;
  double Ss[9];
  double Clusters;
  // Initialize function 'Perform' input arguments.
  // Initialize function input argument 'data'.
  data = argInit_UnboundedxUnbounded_real_T();
  // Initialize function input argument 'data1'.
  data1 = argInit_1xUnbounded_cell_wrap_0();
  // Call the entry-point 'Perform'.
  Perform(data, data1, argInit_real_T(), Result1, &Clusters, Phi, Ss);
}

下面我们进行改写。

3.1 指定输入的文件

需要读取的ranking.mat文件的结构如下:
请添加图片描述

Data1中存储100个字符串大学名称:
在这里插入图片描述

Data中则是一个100x5的分数矩阵:

在这里插入图片描述

我们使用mat.h中的方法,读取mat文件并判断结构:

  const char* matPath= "/Users/mac/Matlab/UniversityRankTest/codegen/lib/Perform/ranking.mat";	
	
  MATFile* pmatFile = matOpen(matPath, "r");

  if (pmatFile == NULL)
  {
    printf("mat数据读取失败!");
    return ;
  }

  matData = matGetVariable(pmatFile, "Data");

  if (matData == NULL)
  {
    printf("结构体中Data数据不存在!");
    return ;
  }

  matData1 = matGetVariable(pmatFile, "Data1");

  if (matData1 == NULL)
  {
    printf("结构体中Data1数据不存在!");
    return ;
  }

  matClose(pmatFile);

3.2 改写处理DataData1结构的数据

下面是改写处理Data的数据(把写死的维度数据,改成变动的实际获取;被注释的部分为原始代码):

coder::array<double, 2U> argInit_UnboundedxUnbounded_real_T(mxArray* data)
{
  coder::array<double, 2U> result;
  // Set the size of the array.
  // Change this size to the value that the application requires.
  
  //原始部分
  result.set_size(2, 2);

  int N = mxGetN(data);
  int M = mxGetM(data);
  result.set_size(M, N);

  double* test = mxGetPr(data);
	
	//原始部分
  // Loop over the array to initialize each element.
//  for (int idx0{0}; idx0 < result.size(0); idx0++) {
//    for (int idx1{0}; idx1 < result.size(1); idx1++) {
//      // Set the value of the array element.
//      // Change this value to the value that the application requires.
//      result[idx0 + result.size(0) * idx1] = argInit_real_T();
//    }
//  }

  for (int idx0{0}; idx0 < M; idx0++) {
    for (int idx1{0}; idx1 < N; idx1++) {
      // Set the value of the array element.
      // Change this value to the value that the application requires.
      result[idx0 + M * idx1] = test[idx0 + M * idx1];
      //        std::cout<<result[idx0 + M * idx1]<<" ";
    }
  }

  return result;
}

下面是改写处理Data1的数据:


coder::array<cell_wrap_0, 2U> argInit_1xUnbounded_cell_wrap_0(mxArray* data)
{
  coder::array<cell_wrap_0, 2U> result;
  // Set the size of the array.
  // Change this size to the value that the application requires.

//原始部分
  result.set_size(1, 2);

  int N = mxGetN(data);
  result.set_size(1, N);

//原始部分
  // Loop over the array to initialize each element.
//  for (int idx0{0}; idx0 < 1; idx0++) {
//    for (int idx1{0}; idx1 < result.size(1); idx1++) {
//      // Set the value of the array element.
//      // Change this value to the value that the application requires.
//      result[idx1] = argInit_cell_wrap_0();
//    }
//  }

  for (int idx1{0}; idx1 < N; idx1++) {
    mxArray * test = mxGetCell(data, idx1);
    result[idx1] = argInit_cell_wrap_0(test);
  }

  return result;
}

//获得元包中的字符串数据
cell_wrap_0 argInit_cell_wrap_0(mxArray* data)
{
  cell_wrap_0 result;
  // Set the value of each structure field.
  // Change this value to the value that the application requires.
  //result.f1 = argInit_1xUnbounded_char_T();

  std::string str = mxArrayToString(data);

  int N = mxGetN(data);

  result.f1 = argInit_1xUnbounded_char_T(str, N);

  return result;
  
  //下面为原始数据
//  cell_wrap_0 result;
//  // Set the value of each structure field.
//  // Change this value to the value that the application requires.
//  result.f1 = argInit_1xUnbounded_char_T();
//  return result;
}

//逐个获取大学字符串数据
coder::array<char, 2U> argInit_1xUnbounded_char_T(std::string data, int N)
{
  coder::array<char, 2U> result;
  // Set the size of the array.
  // Change this size to the value that the application requires.
  result.set_size(1, 2);
  result.set_size(1, N);

  // Loop over the array to initialize each element.
//  for (int idx0{0}; idx0 < 1; idx0++) {
//    for (int idx1{0}; idx1 < result.size(1); idx1++) {
//      // Set the value of the array element.
//      // Change this value to the value that the application requires.
//      result[idx1] = argInit_char_T();
//    }
//  }

  for (int idx1{0}; idx1 < N; ++idx1) {
    result[idx1] = data[idx1];
    //std::cout<<result[idx1]<<" ";
  }

  return result;
}

最后是获得double数组的数据:

//ss数据
  for (int i = 0; i < 9; ++i) {
    SsData[i] = Ss[i];
  }

  bestGroupNum = Clusters;//最佳分组
//phi数据
  for (int idx0{0}; idx0 < 100; idx0++) {
    PhiData[idx0] =  Phi[idx0];
  }

最后,我们来对比一个原始版本(Matlab自动生成的代码)和改写后的代码版本:

原始版本:

static void main_Perform()
{
  coder::array<cell_wrap_0, 2U> data1;
  coder::array<cell_wrap_0, 1U> Result1;
  coder::array<double, 2U> Phi;
  coder::array<double, 2U> data;
  double Ss[9];
  double Clusters;
  // Initialize function 'Perform' input arguments.
  // Initialize function input argument 'data'.
  data = argInit_UnboundedxUnbounded_real_T();
  // Initialize function input argument 'data1'.
  data1 = argInit_1xUnbounded_cell_wrap_0();
  // Call the entry-point 'Perform'.
  Perform(data, data1, argInit_real_T(), Result1, &Clusters, Phi, Ss);
}

改写后的代码版本:

void main_Perform(int iterationNum, const char* matPath, char** univRank, double PhiData[], double SsData[], double& bestGroupNum)
{
  coder::array<cell_wrap_0, 2U> data1;
  coder::array<cell_wrap_0, 1U> Result1;
  coder::array<double, 2U> Phi;
  coder::array<double, 2U> data;
//  double Phi[100];
  double Ss[9];
  double Clusters;

  mxArray* matData = NULL;
  mxArray * matData1 = NULL;

  MATFile* pmatFile = matOpen(matPath, "r");

  if (pmatFile == NULL)
  {
    printf("mat数据读取失败!");
    return ;
  }

  matData = matGetVariable(pmatFile, "Data");

  if (matData == NULL)
  {
    printf("结构体中Data数据不存在!");
    return ;
  }

  matData1 = matGetVariable(pmatFile, "Data1");

  if (matData1 == NULL)
  {
    printf("结构体中Data1数据不存在!");
    return ;
  }

  matClose(pmatFile);

  // Initialize function 'Perform' input arguments.
  // Initialize function input argument 'data'.
  data = argInit_UnboundedxUnbounded_real_T(matData);
  // Initialize function input argument 'data1'.
  data1 = argInit_1xUnbounded_cell_wrap_0(matData1);

  // Call the entry-point 'Perform'.
  Perform(data, data1, iterationNum, Result1, &Clusters, Phi, Ss);

//  for(int i = 0;i < 101;i++){
//    univRank[i] = (char*)malloc(sizeof (char) * 256);
//  }

  for(int i = 0;i < 101;i++){
    univRank[i] = (char*)malloc(sizeof (char) * 256);
  }

  for (int i = 0; i < 101; ++i) {
    std::string tmp = Result1[i].f1.operator std::string();
    strcpy(univRank[i], tmp.c_str());
  }

  for (int i = 0; i < 9; ++i) {
    SsData[i] = Ss[i];
  }

  bestGroupNum = Clusters;

  for (int idx0{0}; idx0 < 100; idx0++) {
    PhiData[idx0] =  Phi[idx0];
  }


}

4 福利

matlab原码、c++转换后代码的资源链接, 提取码: 5vjp

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C++改写Matlab源码实践二之【传递表格数据】————附带详细讲解和示例 的相关文章

  • 使用 Polyfit 进行垂直线拟合

    这只是一个基本问题 我正在使用拟合线来分散点polyfit 在某些情况下 我的散点具有相同的 X 值并且polyfit无法在其上放置一条线 必须有某种东西可以处理这种情况 毕竟 它只是一个线配合 我可以尝试交换 X 和 Y 然后再画一条线
  • 当 MATLAB 发生错误时如何继续循环?

    我正在使用函数将一些 dat 文件转换为 mat 文件 我在循环内调用此函数来转换多个文件 在某些情况下 我的 dat 文件已损坏 函数无法转换并发生错误 从而停止循环 现在我的问题是 是否有任何命令 当错误发生时 它应该跳过循环中的当前
  • 梯度下降Matlab实现

    我已经浏览了堆栈溢出中的许多代码 并在同一行上编写了自己的代码 这段代码有一些问题我无法理解 我正在存储值 theta1 和 theta 2 以及用于分析目的的成本函数 x 和 Y 的数据可以从此下载页 它具有 dat 文件形式的 x 和
  • Matlab,如何获取imagesc生成的结果?

    我读过一些类似的文章 但它们不是我想要的 得到imagesc之后的矩阵 https stackoverflow com questions 14364239 get the matrix after imagesc 14364434 143
  • 读取 MEX 文件中的 4D 数组

    我在 MATLAB 中有一个 4 维数组 我正在尝试访问 MEX 函数中的数组 下面创建 testmatrix 一个 4 维矩阵 已知数据为uint8 type Create a 4D array 2x 2y rgb 3 framenumb
  • 如何在matlab中显示图像上的点?

    我有一些像素点 比如 p1 1 1 和 p2 1 10 等等 我想以任何颜色在图像上显示这些点 这个怎么做 MATLAB plot http www mathworks com help techdoc ref plot html文档非常全
  • 在 MATLAB 中绘制圆

    我被要求找到在 MATLAB 中绘制圆的不同方法 看起来很无聊 不过我可以想出一些想法 有些可能效率低下 Method 1 ezpolar x 1 Method 2 t linspace 0 2 pi 100 plot sin t cos
  • 笛卡尔散点图上的极坐标网格线

    我有一个脚本来创建散点图 使用gscatter 基于另一个脚本生成的 x y 数据 离散数据点 不连续 由于这些数据点实际上是圆形空间中某些对象的位置 因此添加极坐标网格线将使绘图更有意义 有谁知道如何在笛卡尔散点图上显示极坐标网格线 或者
  • MATLAB:解包函数

    我正在与 Mathworks 的某人讨论 unwrap http www mathworks com access helpdesk help techdoc ref unwrap html函数中对于 以外的跳跃容差有一个 bug 并且希望
  • 在Matlab中使用中心切片定理实现滤波反投影算法

    我正在研究一种使用中心切片定理的滤波反投影算法作为家庭作业 虽然我理解纸上的理论 但在 Matlab 中实现它时遇到了问题 我得到了一个可以遵循的框架 但我认为我可能误解了一个步骤 这是我所拥有的 function img sampleFB
  • 如何在 R 中导入 matlab 表

    我有一个matlab mat文件与表数据类型我想将其导入 R 中 我为此使用 readMat R 正在将其作为列表读取 之后有没有办法将列表转换为 R 中的数据帧或表格格式 当我使用as dataframe我收到以下错误 Error in
  • MATLAB:涉及大数的计算

    如何在 MATLAB 中执行涉及大量数字的计算 举一个简单的例子 任意精度计算器将显示 1 120 132 370 260 约为 1 56 但 MATLAB 无法执行此类计算 power 120 132 factorial 370 fact
  • 有没有办法在 Visual Studio 或 MATLAB 中“映射”程序执行顺序?

    我所说的 地图 是指我有一个 主 函数 它调用内部的许多其他程序 我希望能够看到哪个文件首先运行 第二个 第三个等等 基本上 我希望能够请参阅这个大型 OOP 设计程序 创建者没有为其制作 UML 类图 中的依赖项列表和顺序 以帮助破译代码
  • 在Matlab中将矩阵中的元素i,j设置为i*j

    我想生成一个矩阵 其中 i j 元素等于 i j 其中 i j e g 0 2 3 2 0 6 3 6 0 到目前为止 我已经发现我可以使用这个索引矩阵访问非对角线元素 idx 1 eye 3 但我还没有弄清楚如何将矩阵单元的索引合并到计算
  • 数组中 2 个百分位之间的平均元素

    我有 2 个长度为 200 的向量 例如 A 和 B 然后我使用以下方法找到数组 A 的每第二个百分位数 A1 prctile A 1 2 100 1 这样A1是一个长度为50的数组 现在我想找到A1中每两个元素内的A元素的平均值 即A的第
  • 如何在Matlab脚本中将泰勒级数系数存储到数组中

    这个问题是在 m 脚本的上下文中 我知道如何获取函数的泰勒级数 但我没有看到任何命令允许将级数的系数存储到数组中 sym2poly似乎不起作用 如何将系数存储到数组中 例如这个函数 syms x f 1 x 2 4 x 9 我们怎样才能得到
  • 在 Matlab 2014b 中移动等高线图的 z 值

    我正在尝试绘制曲面图 在曲面下方我希望显示轮廓线 但我希望轮廓位于z 1而不是默认值0 我找到了之前关于这个问题的帖子here https stackoverflow com questions 8054966 matlab how to
  • 增加 .fig 文件中的散点标记大小

    我有一个图形文件 scatter fig 该图有许多使用 scatter 的散点绘图仪 现在我只有这个无花果文件 我需要增加所有散点的标记大小 手动尝试过 但非常困难 有没有办法我可以做类似的事情 H 图形句柄 s 点 h 设置 s 标记大
  • 在 MATLAB 中将数据拟合到 B 样条

    我正在尝试估计矩阵形式的时间序列数据中的缺失值 列代表时间点 即现在 我想将矩阵的每一行拟合到 B 样条曲线 并用它来估计缺失值 我可以使用 MATLAB 将数据拟合到普通样条曲线 但我完全陷入尝试找出如何拟合数据以创建 B 样条曲线的困境
  • matlab矩阵中求子矩阵的通用方法

    我正在寻找一种 好 方法来在更大的矩阵 任意维数 中找到矩阵 模式 Example total rand 3 4 5 sub total 2 3 1 3 3 4 现在我希望这样的事情发生 loc matrixFind total sub 在

随机推荐