excel-like formula column
조회 수: 2 (최근 30일)
이전 댓글 표시
matrix of two columns representing in the morning and in the day.
can we have a 3rd column that would compute the average of first 2 columns, and update dynamically when values in first 2 columns change ... how could this be achieved in Matlab?
thx
댓글 수: 0
답변 (1개)
Sulaymon Eshkabilov
2024년 1월 2일
If understood correctly, this is what you are trying to achieve, e.g.:
% Initialize a matrix with two columns of data (Morning and Noon):
Morning = [8; 9; 7; 6; 5];
Noon = [15; 18; 20; 22; 13];
M = [Morning, Noon];
% Function Handle to compute the average dynamically:
Average_Computer = @(M) mean(M, 2);
% Display the initial matrix
disp('Initial Version of the Matrix: ');
disp(M);
% Plot the initial matrix
figure;
h(1) = plot(M(:, 1), 'ro-', 'DisplayName', 'Morning');
hold on;
h(2) = plot(M(:, 2), 'bd-', 'DisplayName', 'Noon');
% Initialize the averaged value is the same as Morning data:
h(3) = plot(M(:, 1), 'kp-.','DisplayName', 'Averaged Data');
legend('show');
title('Morning and Noon Values with Average Values Computed Dynamically');
% Loop based Simulation of dynamic changes :)
for i = 1:5
% Simulate changing values in the first two columns using randi():
M(:, 1) = randi([5, 10], size(M, 1), 1);
M(:, 2) = randi([15, 25], size(M, 1), 1);
% Update the average column:
M(:, 3) = Average_Computer(M);
%Updating the above created plot:
set(h(1), 'YData', M(:, 1));
set(h(2), 'YData', M(:, 2));
set(h(3), 'YData', M(:, 3));
drawnow;
% Pause to see the changes:
pause(1);
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Function Creation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!