Calculate all slopes of arrays in a cell

5 Ansichten (letzte 30 Tage)
Anni Shi
Anni Shi am 15 Aug. 2022
Beantwortet: Image Analyst am 16 Aug. 2022
Hello,
I generated a cell structure with 6 2100*4 arrays.
I want to calculate the slope of the first(x) and the second column (y) of each array in the cell.
Is there any way to operate individual element in a cell and return the list of outputs?
Thank you?
  3 Kommentare
David Hill
David Hill am 15 Aug. 2022
Attach your data if you can.
Anni Shi
Anni Shi am 15 Aug. 2022
Data file is attached. Thank you for the reminder!

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 16 Aug. 2022
Try this:
% Demo by Image Analyst
% Initialization Steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
s = load('test_data.mat')
Allmsd = s.Allmsd
[rows, columns] = size(Allmsd)
slopes = zeros(rows, 1);
subplot(2, 1, 1);
for k=1:numel(Allmsd)
thisArray = Allmsd{k};
x = thisArray(:, 1);
y = thisArray(:, 2);
% Get rid of nans.
badIndexes = isnan(x) | isnan(y);
x(badIndexes) = [];
y(badIndexes) = [];
plot(x, y, '-', 'LineWidth', 2);
grid on;
hold on;
coefficients = polyfit(x, y, 1)
slopes(k) = coefficients(1)
end
legend
title('Individual Curves', 'FontSize', fontSize)
% Plot slopes
subplot(2, 1, 2);
bar(slopes)
grid on;
title('Slopes', 'FontSize', fontSize)

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by