Clear Filters
Clear Filters

How Can I Make a Matlab Code generate multiple separate matrices

8 views (last 30 days)
I want to generate 21 matrices from a code that are 4x4 with values in place that I can set elsewhere in the code. I don't want to type out 21 different 4x4 matrices.

Answers (2)

Jos (10584)
Jos (10584) on 30 Mar 2015
Edited: Jos (10584) on 30 Mar 2015
To generate one 4-by-4 matrix with a specific value you can use various approaches
Value = 3 ;
A = repmat(Value,4,4)
B = Value * ones(4,4)
C = zeros(4,4), C(:) = Value
You have to think about how to store several of these matrices. As planes in 3D arrays or, for instance, as cells in a cell array? The first option can only be used when all 2D matrices have the same size
Values = [3 6 99] ;
N = numel(Values)
Array3D = zeros(4,4,N) % a 4-by-4-by-3 3D array
for k=1:N
Array3D(:,:,k) = Values(k)
end
or in a cell array
Values = [7 -3 5 4]
CellArray = arrayfun(@(x) repmat(x,4,4),Values,'un',0)
disp(CellArray{1})
  7 Comments
Declan Simons
Declan Simons on 30 Mar 2015
The values are to be generated by the user in a GUI with relative importance to each of the other criteria available.
Also another quick question I have a table that I want to display in the GUI but it displays as an external figure. Any thoughts?
function pushbutton5_Callback(hObject, eventdata, handles)
comp1 = str2num(char(get(handles.edit7,'String')));
comp2 = str2num(char(get(handles.edit8,'String')));
comp3 = str2num(char(get(handles.edit9,'String')));
comp4 = str2num(char(get(handles.edit10,'String')));
comp5 = str2num(char(get(handles.edit11,'String')));
comp6 = str2num(char(get(handles.edit12,'String')));
comparison = [1,comp1,comp2,comp3;
(comp1)^-1, 1,comp4,comp5;
(comp2)^-1,(comp4)^-1,1,comp6;
(comp3)^-1,(comp5)^-1,(comp6)^-1,1]
f = figure('Position', [100 100 752 250]);
t = uitable('Parent', f, 'Position', [25 50 700 200], 'Data', comparison)
get(handles.uitable5,'Comparison',comparison,'ColumnName',colnames);
function evaluate_Callback(hObject, eventdata, handles)
% f = figure('Position', [100 100 752 250]);
% t = uitable('Parent', f, 'Position', [25 50 700 200], 'Data', comparison)
Stephen23
Stephen23 on 30 Mar 2015
Edited: Stephen23 on 30 Mar 2015
Read the figure documentation. Every time you call figure('Pos..',...) it will create a new figure. Instead create the figure once and then pass its handle to the plot calls and other GUI functions.

Sign in to comment.


Stephen23
Stephen23 on 30 Mar 2015
Edited: Stephen23 on 12 Sep 2023
Use a cell array like this:
out = cell(1,21);
for trial = 1:21
out{trial} = ... your calculation here
end

Categories

Find more on Language Fundamentals in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!