Saving matrices within a cell
4 ビュー (過去 30 日間)
古いコメントを表示
Hello!
I want to save all the variable matrices "matrix_points" (which I create inside the function "function_main.m") inside a cell.
I hope I can explain myself better...
I have these lines of code:
X % matrix [rows x 2 columns] double
matrix_points = function_main(X);
Within the function "function_main.m" there is this part of code where I then derive "matrix_points":
count_rows = height(px); % px is an array count_rows x columns
if count_rows == 2
matrix_points = function_1(input);
elseif count_rows == 3
matrix_points = function_2(input);
elseif count_rows == 4
matrix_points = function_3(input);
end
I want to save all the variable matrices "matrix_points" (which I create inside the function "function_main.m") inside a cell.
0 件のコメント
採用された回答
Image Analyst
2022 年 12 月 3 日
編集済み: Image Analyst
2022 年 12 月 3 日
Initialize your matrix for all the matrixes
allMatrixes = {};
Then after your if block just append your latest matrix:
allMatrixes = [allMatrixes; {matrix_points}];
4 件のコメント
Image Analyst
2022 年 12 月 3 日
Try this. It should work.
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;
allMatrixes = {};
for k = 1 : 4
% Create sample px
numPxRows = 1 + randi(3);
px = rand(numPxRows, 2);
count_rows = height(px); % px is an array count_rows x columns
inputArgument = randi(10);
if count_rows == 2
fprintf('count_rows = 2 so calling function_1(%d).\n', inputArgument)
matrix_points = function_1(inputArgument);
elseif count_rows == 3
fprintf('count_rows = 3 so calling function_2(%d).\n', inputArgument)
matrix_points = function_2(inputArgument);
elseif count_rows == 4
fprintf('count_rows = 4 so calling function_3(%d).\n', inputArgument)
matrix_points = function_3(inputArgument);
end
fprintf(' Adding a cell with a %d element vector in it.\n\n', length(matrix_points))
allMatrixes = [allMatrixes; {matrix_points}];
end
% Display it
celldisp(allMatrixes)
%===========================================================================
% Define all functions below.
function output = function_1(n)
output = rand(1, n);
end
function output = function_2(n)
output = 2 * rand(1, n);
end
function output = function_3(n)
output = 3 * rand(1, n);
end
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Creating, Deleting, and Querying Graphics Objects についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!