AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题

问题[matlab](coding)

Martin Hope
ysap
Asked: 2025-04-30 05:15:36 +0800 CST

如何更改图例中的线条和标记样式?

  • 6

我的脚本生成了一个由数十条线和线段组成的复合图。我尝试为该图添加一个自定义图例,该图例仅包含其中三条线,但我似乎无法控制该图例中线条和标记的样式。

情节如下:

在此处输入图片描述

在图例中,我希望Line1风格 为'b.-',Line2风格 为'r-o',Line3风格 为'g-o'。

有没有办法用一定数量的标签和特定的线条样式来定义图例?

matlab
  • 1 个回答
  • 30 Views
Martin Hope
minh chanh le
Asked: 2025-03-04 23:00:10 +0800 CST

在 SVM Matlab 中使用多项式核函数绘制决策边界

  • 5

我正在尝试在 Matlab 上为 fisherisis 数据集绘制决策边界。以下是我正在处理的代码:

load fisheriris;
X = meas(:,1:2);
y = species;

% PLOT
figure;
plot(X(strcmp(y,"setosa"),1),X(strcmp(y,"setosa"),2),'.','DisplayName','setosa'); hold on
plot(X(strcmp(y,"versicolor"),1),X(strcmp(y,"versicolor"),2),'.','DisplayName','versicolor');
plot(X(strcmp(y,"virginica"),1),X(strcmp(y,"virginica"),2),'.','DisplayName','virginica'); hold off
legend("setosa","versicolor","virginica")

% SVM
t = templateSVM("KernelFunction","polynomial", "BoxConstraint",1,"KernelScale","auto","PolynomialOrder",2,"Standardize",1);
SVMModel = fitcecoc(X,y,"Learners", t);

% DECISION BOUNDARY
%%% Draw decision boundary here

我使用的上述 SVMModel 是分类多类 SVM(3 个类:“setosa”、“versicolor”、“virginica”)和数据集的第一列 2 个用于训练数据。 上述代码的图

我希望可以绘制 2 或 3 条线或曲线来显示 SVMModel 中每个物种的边界。我读过几个网站,它们在某种程度上解决了我的问题,但对我来说都不起作用,因为它们使用的是线性 SVM 或线性方程... 以下是我搜索过的网站列表:

  1. 第一个网站,但这是用于二元分类的,在我的例子中,我有 3 个类。
  2. 第二个网站,但这是针对线性 SVM 的,在我的情况下是多项式核函数。
  3. 第三个网站,但在这种情况下,的属性ecoc具有属性Beta,在我的情况下Beta没有提供。

有人知道在我的处境下如何绘制决策边界吗?提前谢谢大家。

matlab
  • 1 个回答
  • 20 Views
Martin Hope
Edwin
Asked: 2025-02-27 18:28:32 +0800 CST

八度轮廓函数返回未接受的错误消息

  • 6

我一直试图解决这个问题,但至今没有任何进展。

从 Coursera 课程“使用 MATLAB 进行数据、信号和图像分析简介”中检索到以下代码。我正在尝试看看是否可以在 Octave 9.2 中运行它。但这会导致错误:

错误:contourc:X、Y 和 Z 必须是实数数字矩阵错误:从 contourc 调用,位于第 110 行第 5 列,位于第 146 行第 14 列,位于第 80 行第 18 列,位于第 19 行第 3 列的轮廓,位于分类 1

X、Y 和 cls 的类别为 double,且所有 3 个的大小均为 4x8。

阅读这篇文章:八度轮廓图轮廓函数工作正常。没有错误。但是使用代码:

function classification1()
  load fisheriris
  pkg load statistics
  [~,~,s] = unique(species);
  figure(1);
  plot(meas(s==1 ,1), meas(s==1, 2), 'rx')
  hold on
  plot(meas(s==2 ,1), meas(s==2, 2), 'go')
  plot(meas(s==3 ,1), meas(s==3, 2), 'b*')
  xlabel('Feature 1');
  ylabel('Feature 2');
  legend('setosa', 'versicolor', 'virginica');
  [X,Y] = meshgrid(4:.01:8, 2:.01:4.5);
  cls = my_iris_classifier([X(:), Y(:)]);
  cls = reshape(cls, size(X));
  contour(X, Y, cls==1,[1,1], 'r--');
  contour(X, Y, cls==2, [1, 1], 'g--');
  contour(X, Y, cls==3, [1, 1], 'b--');

endfunction

function class = my_iris_classifier(feat)
    class = zeros(size(feat,1),1);
    for i = 1:size(feat,1);
      if feat(i,1) > 6
        class(i) = 3;
      elseif feat(i,2) > 3
        class(i) = 1;
      else
        class(i) = 2;
      endif
    endfor

endfunction

但事实并非如此。

matlab
  • 1 个回答
  • 27 Views
Martin Hope
SH_IQ
Asked: 2024-12-25 01:56:56 +0800 CST

在 MATLAB 条形图中为特定条形颜色添加附加图例

  • 7

我有以下 MATLAB 代码,它直观地显示了 DCO-OFDM 和 m-CAP 在覆盖范围与 QAM 阶数方面的差异。条形表示各种配置下的覆盖范围,其中一些条形段为红色和蓝色,表示特定效果(例如 LOS+NLOS)。

当前图例已经描述了配置(例如,m-CAP 和 DCO-OFDM 的角度)。但是,我想为红色和蓝色条段添加两个额外的图例条目来解释它们的重要性,如附图所示。

目标是在现有图例下添加两个额外的图例,代表:

  • 红色条:LOS+NLOS 对 m-CAP 的影响。
  • 蓝色条:LOS+NLOS 对 DCO-OFDM 的影响。

以下图像清楚地说明了所需的图例布局:

在此处输入图片描述

我可以得到帮助来实现这一点吗?具体来说:

  • 如何在不破坏现有图例的情况下为这些条形颜色添加红色和蓝色图例?
  • 有没有更有效的方法来实现这个结果?提前感谢您的帮助!

下面是我用于创建条形图的 MATLAB 代码示例:

close all;
clear variables;
clc;

QAM = [16, 32, 64, 128, 256, 512, 1024];

%% m_CAP modulation

% Beamsteering without reflection
VEC_Beam_no_reflection_mCAP = [
    117.5491   75.1933   53.5396   38.3105   23.0815   13.5634    6.9007; % Theta = 10
    118.2629   76.1452   54.0155   39.0244   23.0815   13.5634    6.9007; % Theta = 30
    118.2629   76.3831   54.0155   39.5003   22.8435   13.5634    6.9007; % Theta = 45
    118.2629   76.6211   54.2534   39.5003   22.8435   13.5634    6.9007  % Theta = 60
];

% Beamsteering with reflection
VEC_Beam_reflection_mCAP = [
    121.3563   77.0970   54.7293   39.7383   22.8435   13.5634    6.9007; % Theta = 10
    141.1065   88.7567   61.6300   42.5937   24.2713   14.5152    7.8525; % Theta = 30
    153.7180   95.6573   67.8168   45.6871   26.6508   14.5152    8.8043; % Theta = 45
    160.3807  100.6544   69.7204   50.4462   28.7924   16.4188    8.8043  % Theta = 60
];

%% DCO-OFDM modulation

% Beamsteering without reflection
VEC_Beam_no_reflection_OFDM = [
    91.3742   59.7264   42.1178   28.7924   16.4188    8.8043    4.9970; % Theta = 10
    95.4194   58.2986   34.7412   25.9369   16.4188   10.7079    4.9970; % Theta = 30
    89.4706   59.4884   44.4973   24.7472   16.4188    8.8043    4.9970; % Theta = 45
    83.5217   69.4825   40.2142   28.7924   16.4188   10.7079    4.9970  % Theta = 60
];

% Beamsteering with reflection
VEC_Beam_reflection_OFDM = [
    94.7055   61.8679   42.1178   28.7924   16.4188    8.8043    4.9970; % Theta = 10
    113.9798   69.7204   42.1178   28.7924   16.4188   10.7079    4.9970; % Theta = 30
    115.6454   74.9554   55.6811   28.7924   16.4188    9.7561    4.9970; % Theta = 45
    114.4557   88.7567   50.6841   34.5033   21.1779   11.6597    4.9970  % Theta = 60
];

%% Combine Data for Bar Chart
angles = [10, 30, 45, 60];
data_no_reflection = [VEC_Beam_no_reflection_mCAP; VEC_Beam_no_reflection_OFDM];
data_with_reflection = [VEC_Beam_reflection_mCAP; VEC_Beam_reflection_OFDM];

% Generate gradient colors for m-CAP (blue) and DCO-OFDM (green)
mCAP_blue_gradient = [linspace(0.8, 0.1, 4)', linspace(0.9, 0.2, 4)', linspace(1.0, 0.3, 4)']; % Light to dark blue
DCO_green_gradient = [linspace(0.7, 0.1, 4)', linspace(0.9, 0.4, 4)', linspace(0.6, 0.2, 4)']; % Light to dark green

% Combine colors
custom_colors = [mCAP_blue_gradient; DCO_green_gradient];

f = figure('Position', [100, 100, 800, 600]);

% Set "HandleVisibility" to off so these bars don't appear in legend
b2 = bar(data_with_reflection', 'grouped','HandleVisibility','off');
for k = 1:length(b2)
    b2(k).FaceColor = 'flat';
    b2(k).CData = repmat([1,0,0], size(b2(k).CData, 1), 1); % [1,0,0] for red bars
end

% Plot for No Reflection
hold on; % To plot 2nd set of bars on top

b1 = bar(data_no_reflection', 'grouped');
for k = 1:length(b1)
    b1(k).FaceColor = 'flat';
    b1(k).CData = repmat(custom_colors(k, :), size(b1(k).CData, 1), 1);
end

% Adjust axis and grid
grid on;
xticks(1:length(QAM));
xticklabels(string(QAM));
xlabel('QAM Order, M');
ylabel('Coverage Area (m²)');
title({'Coverage Area With Reflection', 'm-CAP and DCO-OFDM'});

% Update the legend
legend(["m-CAP \Phi_{1/2}=10°", "m-CAP \Phi_{1/2}=30°", "m-CAP \Phi_{1/2}=45°", "m-CAP \Phi_{1/2}=60°", ...
        "DCO-OFDM \Phi_{1/2}=10°", "DCO-OFDM \Phi_{1/2}=30°", "DCO-OFDM \Phi_{1/2}=45°", "DCO-OFDM \Phi_{1/2}=60°"], ...
       'Location', 'Best');
matlab
  • 1 个回答
  • 20 Views
Martin Hope
SH_IQ
Asked: 2024-12-12 20:49:01 +0800 CST

如何在 MATLAB 中突出显示两个条形图之间的差异?

  • 6

我正在从事一个 MATLAB 项目,其中有两组数据:

1-“无反射”:表示基线覆盖面积值。2-“有反射”:表示具有附加反射效果的覆盖面积值。

我将这两个数据集绘制为分组条形图,并希望突出显示反射造成的差异。具体来说,我需要在“无反射”条形图上方以不同颜色突出显示与差异(反射效果)相对应的每个条形图部分。

在此处输入图片描述

示例: 例如,如果:

  • 第一个条(QAM = 16)的“无反射”值为 120 平方米。
  • 同一酒吧的“含反射”值为 160 平方米。

我想要:

1- 绘制一个代表 120 平方米(“无反射”的基准值)的条形图。

2- 用红色突出显示蓝色条顶部的差异(160 - 120 = 40 平方米)(突出显示反射效果)。

我想将此应用到右侧图片上的所有条形图上。有什么可以帮忙的吗?

这是我的代码:

close all;
clear variables;
clc;

QAM = [16, 32, 64, 128, 256, 512, 1024];

%% m_CAP modulation

% Beamsteering without reflection
VEC_Beam_no_reflection_mCAP = [
    117.5491   75.1933   53.5396   38.3105   23.0815   13.5634    6.9007; % Theta = 10
    118.2629   76.1452   54.0155   39.0244   23.0815   13.5634    6.9007; % Theta = 30
    118.2629   76.3831   54.0155   39.5003   22.8435   13.5634    6.9007; % Theta = 45
    118.2629   76.6211   54.2534   39.5003   22.8435   13.5634    6.9007  % Theta = 60
];

% Beamsteering with reflection
VEC_Beam_reflection_mCAP = [
    121.3563   77.0970   54.7293   39.7383   22.8435   13.5634    6.9007; % Theta = 10
    141.1065   88.7567   61.6300   42.5937   24.2713   14.5152    7.8525; % Theta = 30
    153.7180   95.6573   67.8168   45.6871   26.6508   14.5152    8.8043; % Theta = 45
    160.3807  100.6544   69.7204   50.4462   28.7924   16.4188    8.8043  % Theta = 60
];

%% DCO-OFDM modulation

% Beamsteering without reflection
VEC_Beam_no_reflection_OFDM = [
    91.3742   59.7264   42.1178   28.7924   16.4188    8.8043    4.9970; % Theta = 10
    95.4194   58.2986   34.7412   25.9369   16.4188   10.7079    4.9970; % Theta = 30
    89.4706   59.4884   44.4973   24.7472   16.4188    8.8043    4.9970; % Theta = 45
    83.5217   69.4825   40.2142   28.7924   16.4188   10.7079    4.9970  % Theta = 60
];

% Beamsteering with reflection
VEC_Beam_reflection_OFDM = [
    94.7055   61.8679   42.1178   28.7924   16.4188    8.8043    4.9970; % Theta = 10
    113.9798   69.7204   42.1178   28.7924   16.4188   10.7079    4.9970; % Theta = 30
    115.6454   74.9554   55.6811   28.7924   16.4188    9.7561    4.9970; % Theta = 45
    114.4557   88.7567   50.6841   34.5033   21.1779   11.6597    4.9970  % Theta = 60
];

%% Combine Data for Bar Chart
angles = [10, 30, 45, 60];
data_no_reflection = [VEC_Beam_no_reflection_mCAP; VEC_Beam_no_reflection_OFDM];
data_with_reflection = [VEC_Beam_reflection_mCAP; VEC_Beam_reflection_OFDM];

% Generate gradient colors for m-CAP (blue) and DCO-OFDM (green)
mCAP_blue_gradient = [linspace(0.8, 0.1, 4)', linspace(0.9, 0.2, 4)', linspace(1.0, 0.3, 4)']; % Light to dark blue
DCO_green_gradient = [linspace(0.7, 0.1, 4)', linspace(0.9, 0.4, 4)', linspace(0.6, 0.2, 4)']; % Light to dark green

% Combine colors
custom_colors = [mCAP_blue_gradient; DCO_green_gradient];

% Plot for No Reflection
figure('Position', [100, 100, 800, 600]);
b1 = bar(data_no_reflection', 'grouped');
for k = 1:length(b1)
    b1(k).FaceColor = 'flat';
    b1(k).CData = repmat(custom_colors(k, :), size(b1(k).CData, 1), 1);
end
xticks(1:length(QAM));
xticklabels(string(QAM));
xlabel('QAM Order, M');
ylabel('Coverage Area (m²)');
title({'Coverage Area Without Reflection', 'm-CAP and DCO-OFDM'});
legend(["m-CAP \Phi_{1/2}=10°", "m-CAP \Phi_{1/2}=30°", "m-CAP \Phi_{1/2}=45°", "m-CAP \Phi_{1/2}=60°", ...
        "DCO-OFDM \Phi_{1/2}=10°", "DCO-OFDM \Phi_{1/2}=30°", "DCO-OFDM \Phi_{1/2}=45°", "DCO-OFDM \Phi_{1/2}=60°"], ...
       'Location', 'Best');
grid on;

% Plot for With Reflection
figure('Position', [100, 100, 800, 600]);
b2 = bar(data_with_reflection', 'grouped');
for k = 1:length(b2)
    b2(k).FaceColor = 'flat';
    b2(k).CData = repmat(custom_colors(k, :), size(b2(k).CData, 1), 1);
end
xticks(1:length(QAM));
xticklabels(string(QAM));
xlabel('QAM Order, M');
ylabel('Coverage Area (m²)');
title({'Coverage Area With Reflection', 'm-CAP and DCO-OFDM'});
legend(["m-CAP \Phi_{1/2}=10°", "m-CAP \Phi_{1/2}=30°", "m-CAP \Phi_{1/2}=45°", "m-CAP \Phi_{1/2}=60°", ...
        "DCO-OFDM \Phi_{1/2}=10°", "DCO-OFDM \Phi_{1/2}=30°", "DCO-OFDM \Phi_{1/2}=45°", "DCO-OFDM \Phi_{1/2}=60°"], ...
       'Location', 'Best');
grid on;
matlab
  • 1 个回答
  • 26 Views
Martin Hope
zajer
Asked: 2024-11-06 02:37:45 +0800 CST

当存在多个解时,Matlab mldivide 返回 NaN

  • 6

我有两个矩阵:

A = [ -1 0  0;
       1 1 -1;
       0 -1 1 ];
B = [-1; 0; 1];

我想解下列方程:

Ax=B

当我使用mldivide函数时,我得到一个 NaN 矩阵

X = mldivide(A,B)
X =

   NaN
   NaN
   NaN

知道该问题有多种解决方案,我手动测试了其中一种,即[1;0;1]返回B:

A*[1; 0; 1]

并且,正如预期的那样,我确信它是多个解决方案之一。所以我的问题是:为什么mldivide返回错误的解决方案?

matlab
  • 1 个回答
  • 22 Views
Martin Hope
user28134562
Asked: 2024-11-05 15:25:50 +0800 CST

如何在另一个 .m 文件中访问 MATLAB UI 函数?

  • 5

我正在尝试通过将一些初始化分离到单独的 .m 文件中来整理我的 MATLAB 应用程序代码。为此,我为每种类型的组件设置了各种文件(例如按钮、图形等的文件)。我正尝试从按钮文件访问主初始化文件中的函数。我的代码在按钮 .m 文件中如下所示:

classdef buttons < handle
    methods
        %initializes the UI
        function buttonCreate(app)
           
            %Create file load 1
            app.fileload1 = uibutton(app.gridLayout, 'push');
            app.fileload1.FontSize = 36;
            app.fileload1.Layout.Row = [8 9];
            app.fileload1.Layout.Column = 1;
            app.fileload1.Text = 'Load 1';

            %proceeds to create the rest of the buttons
        end
    end
end

现在我尝试访问buttonCreate()主初始化文件中的函数initialize.m:

classdef initialize < handle
    properties 
        fig
        gridLayout
        axes

        fileload1
    end
    methods
        %initializes the UI
        function init(app)
            %create canvas
            import buttons.*;
            fig = uifigure;
            fig.Position = [100 100 1920 1080];
            movegui(fig,'center');
            fig.Name = "Audio Editor";

            %Create grid layout
            gridLayout = uigridlayout(fig);
            gridLayout.ColumnWidth = {'1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x'};
            gridLayout.RowHeight = {'1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x'};

            buttonCreate(app);

        end
    end
    %code for calling and deleting
    methods
        %calls code to create canvas upon app start
        function app = initialize
            init(app)
        end
        %removes the app and deletes app.fig
        function delete(app)
            delete(app.fig);
        end
    end
end
Error in initialize/init (line 41)
            buttonCreate(app);
            ^^^^^^^^^^^^^^^^^^
Error in initialize (line 54)
            init(app)
            ^^^^^^^^^

这导致程序UIFigure仍在创建,但没有按钮,并且终端出现上述错误。

matlab
  • 1 个回答
  • 20 Views
Martin Hope
Andrew Geistfeld
Asked: 2024-10-29 01:31:15 +0800 CST

反向二维表查找

  • 6

我正在模拟一个 APM(自动电源管理)系统,其中机器调整其齿轮和 RPM 以对可变工作负载做出反应。我以拖拉机为模型。因此,对于此模型,可变工作负载将增加/减少拖拉机必须拉动的阻力。

我被变速箱难住了。我有确定所需车轮扭矩的代码,我需要进行逆表查找。表格设置为 f(x,y)=z,其中 x=油门位置/转速,y=档位,z=车轮扭矩。在这种情况下,我知道我的油门位置,也知道所需的扭矩,我需要一种方法让它给我相应的档位。

该表如下所示:

桌子

我知道如何使用二维表查找从 x 和 y 中找到 z,但不知道如何使用 x 和 z 来查找 y。

matlab
  • 1 个回答
  • 36 Views
Martin Hope
qtqt
Asked: 2024-10-19 13:49:21 +0800 CST

查找矩阵中重复值的列索引

  • 5

我使用的是 Matlab 符号。我有一个大小为 (Nx3) 的矩阵 A,其中 A 的每个条目都是正整数。我想创建一个维度为 (max(max(A))x2) 的矩阵 Q。Q 的第 i 行对应于 A 的列索引,因此 A(k,r) = i。因此,如果 A(k_1,r_1) = i 且 A(k_2,r_2) = i,我们可以设置 Q(i,:)=[k_1,k_2]。对于 A 的唯一条目,Q 的相关行的每一列都相等。

有没有办法将任务矢量化?我可以使用循环执行任务,但在 MATLAB 中效率很低(当矩阵 A 有很大的行并且 max(max(A)) 也很大时)。这是一个例子。

A=[  1    10     9
     1     2     4
     3     2    11
     3    12     5
     6    13     4
     6     7    15
     8     7     5
     8    14    16]

在上面的例子中,预期结果是

Q =
     1     1
     2     2
     1     1
     3     3
     3     3
     1     1
     2     2
     1     1
     3     3
     2     2
     3     3
     2     2
     2     2
     2     2
     3     3
     3     3

这就是我的慢速代码所做的

max_Q = max(max(A));
Q     = zeros(max_Q,2);
for i = 1 : max_Q
    [~,cols] = find(i == A); 
    Q(i,:) = cols;
end
matlab
  • 1 个回答
  • 36 Views
Martin Hope
Varga
Asked: 2024-10-19 01:19:20 +0800 CST

波浪模拟过程中大规模计算的优化与加速

  • 5

有一个任务:模拟海面上的波浪(3D 情况)。我遇到的问题是,当模拟大片海洋(1 公里 x 1 公里)的波浪时,程序需要很长时间才能执行。事实上它根本没有执行。因为在等待结果的半个小时里,它从未被计算过。

我的代码示例:

clear,clc
%% Data 
x = 0:1:100; % coordinates х [m]
y = 0:1:100; % coordinates y [m]
g = 9.81; % gravitational constant [m/s^2]
speed = 5; % wind velocity [m/s]
w0 = (g / speed); % norm.frequency [Hz]
dw = 0.1; % frequency step [rad/s]
w = 0.8:dw:11.1; % frequency [rad/s] 
dtt = pi / 18; % angular step [rad]
theta = 0:dtt:pi; % direction angles, angles between the wavevector & coordintae axis [rad]

%% P-M spectrum, Frequency-Angular spectrum & Amplitude
Psi = 8.1e-3 .* ((w/w0).^(-5)) .* exp((-0.74) ./ ((w/w0).^(4))); % P-M spectrum [none]
Phi = ((speed)^(5)/g^(3)) * Psi; % self-similar spectrum [s*m^2]
Sw = Phi / 2; % frequency spectrum [s*m^2]

St = cos(theta).^(4); % angular spectrum [none]
Norm = trapz(dtt, St); % norm.coefficient [none]
Swt = Sw .* St'; % frequency-angular spectrum [s*m^2]

eta0 = sqrt((Swt * dw * dtt) ./ Norm); % amplitude [m]

figure(1);
subplot(2,1,1)
plot(w, Psi);
title('$$\Psi$$($$\omega$$) - P-M spectrum', 'Interpreter', 'LaTex');
xlabel('\omega [rad/s]');
ylabel('\Psi [none]');
grid on;
subplot(2,1,2)
plot(w, Swt); 
title('$$S(\omega , \theta)$$($$\omega$$) - frequency-angular spectrum', 'Interpreter', 'LaTex');
xlabel('\omega [rad/s]');
ylabel('S(\omega,\theta) [s*m^2]');
grid on;

%% Setting the initial phase parameter
phase = 2*pi*rand(length(theta),length(w)); %% random initial phase ranging from 0 to 2pi [rad]

%% Surface Waving [Linear, 3D (eta & x,y)] at different harmonics & random phase (at one moment in time), different directions of the wavevector (multiple angles)
t = 0; % time moment [s]
Kabs = (w.^2) / g; % wavevector module [rad/m]
Kx = Kabs .* cos(theta)'; % projection of the wavevector onto the x-axis [rad/m]
Ky = Kabs .* sin(theta)'; % projection of the wavevector onto the y-axis [rad/m]

eta = zeros(length(x),length(y),length(theta),length(w)); % reserving space for calculation results
tic
for i = 1:length(x)
    for j = 1:length(y)
        eta(i,j,:,:) = eta0 .* cos(w * t - Kx .* i - Ky .* j + phase);
    end
end
toc
% sum(sum(eta,4),3) - double sum of eta over all harmonics (frequencies) and wavevector directions (angles theta),
% where '4' и '3' summation indicator for variable frequency and angle
etaW = sum(eta,4);
etaWA = sum(etaW,3);

figure(2)
surf(x,y,etaWA);
title('\eta(x,y) - surface waving');
xlabel('x [m]');
ylabel('y [m]');
zlabel('\eta [m]');
cbar = colorbar;
cbar.Label.String = '\eta [m]';
grid on
shading flat

我能够使用的代码优化方法之一是创建一个“空”的 4D 数组(零的 4D 数组)eta = zeros(length(x),length(y),length(theta),length(w));,在执行循环后,计算结果将填充到该数组中:

eta = zeros(length(x),length(y),length(theta),length(w)); % reserving space for calculation results
tic
for i = 1:length(x)
    for j = 1:length(y)
        eta(i,j,:,:) = eta0 .* cos(w * t - Kx .* i - Ky .* j + phase);
    end
end
toc

然后我按频率和角度变量总结结果:

etaW = sum(eta,4);
etaWA = sum(etaW,3);

这样就提前为结果准备好了地方。这有点帮助。例如,x = 0:1:100; y = 0:1:100;使用此方法对一个区域(100 mx 100 m)的代码执行时间为 0.7 秒(不使用则为 3.9 秒)。对于一个区域x = 0:1:500; y = 0:1:500;(500 mx 500 m),执行时间约为 19 秒(不使用则...我不知道,我没有等待代码执行,但结果发现它很长)。但是,对于一个区域x = 0:1:1000; y = 0:1:1000;(1000 mx 1000 m),我很长时间都没有得到想要的结果(感觉根本得不到)。

在我的情况下,还有其他方法可以实现所需的结果并优化我的代码,以便它可以应对如此规模的数据(同时,无需改变数组中的步骤)?

注意:我的电脑有 16 GB 的 RAM。我的第二台电脑(我最初用来进行计算的那台电脑)在执行程序时完全挂断了,所以我不得不“手动”重启它。所以我猜它的 RAM 更少了。

matlab
  • 1 个回答
  • 36 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve