我的脚本生成了一个由数十条线和线段组成的复合图。我尝试为该图添加一个自定义图例,该图例仅包含其中三条线,但我似乎无法控制该图例中线条和标记的样式。
情节如下:
在图例中,我希望Line1
风格 为'b.-'
,Line2
风格 为'r-o'
,Line3
风格 为'g-o'
。
有没有办法用一定数量的标签和特定的线条样式来定义图例?
我正在尝试在 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 或线性方程... 以下是我搜索过的网站列表:
ecoc
具有属性Beta
,在我的情况下Beta
没有提供。有人知道在我的处境下如何绘制决策边界吗?提前谢谢大家。
我一直试图解决这个问题,但至今没有任何进展。
从 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 代码,它直观地显示了 DCO-OFDM 和 m-CAP 在覆盖范围与 QAM 阶数方面的差异。条形表示各种配置下的覆盖范围,其中一些条形段为红色和蓝色,表示特定效果(例如 LOS+NLOS)。
当前图例已经描述了配置(例如,m-CAP 和 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-“无反射”:表示基线覆盖面积值。2-“有反射”:表示具有附加反射效果的覆盖面积值。
我将这两个数据集绘制为分组条形图,并希望突出显示反射造成的差异。具体来说,我需要在“无反射”条形图上方以不同颜色突出显示与差异(反射效果)相对应的每个条形图部分。
示例: 例如,如果:
我想要:
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;
我有两个矩阵:
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
返回错误的解决方案?
我正在尝试通过将一些初始化分离到单独的 .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
仍在创建,但没有按钮,并且终端出现上述错误。
我正在模拟一个 APM(自动电源管理)系统,其中机器调整其齿轮和 RPM 以对可变工作负载做出反应。我以拖拉机为模型。因此,对于此模型,可变工作负载将增加/减少拖拉机必须拉动的阻力。
我被变速箱难住了。我有确定所需车轮扭矩的代码,我需要进行逆表查找。表格设置为 f(x,y)=z,其中 x=油门位置/转速,y=档位,z=车轮扭矩。在这种情况下,我知道我的油门位置,也知道所需的扭矩,我需要一种方法让它给我相应的档位。
该表如下所示:
我知道如何使用二维表查找从 x 和 y 中找到 z,但不知道如何使用 x 和 z 来查找 y。
我使用的是 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
有一个任务:模拟海面上的波浪(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 更少了。