Output Structure Values Of Unequal Length To A Matrix

3 Ansichten (letzte 30 Tage)
Michael Mancini
Michael Mancini am 23 Nov. 2020
Bearbeitet: Voss am 7 Sep. 2024
Lets say I have a structure (st) of of three runs depicted below which contains the following fields: run, x and y. run is only a scalar and x & y are the same length for each corresponding run. The following code is an example
st(1).run = 1; st(1).x = [0:2]'; st(1).y = 1*st(1).x;
st(2).run = 2; st(1).x = [5:7]'; st(2).y = 2*st(2).x;
st(3).run = 3; st(1).x = [0:2]'; st(3).y = 3*st(3).x;
I can generate the output that combines the entire structure into one matrix by using the following code which works
rightOutputMat = [];
for i = 1:length(s)
tempRun = ones(length(st(i).x),1)*st(i).run;
rightOutputMat = [rightOutputMat; tempRun st(i).x st(i).y];
end
However is it possible to generate an output vector without using a for loop. For example i can use the following to concatenate st.x & st.y into one matrix
outputMat = [vertcat(st.x) vertcat(st.y)];
but I am struggling to include the run number since it is only a scalar with length of 1 and x & y are the same lengths for
Thanks

Antworten (1)

Deepak
Deepak am 6 Sep. 2024
To my understanding, you have created a structure that contains three fields: “run”, “x”, and “y”. Now, you want to combine the entire structure into one matrix without explicitly using a for loop.
To accomplish this task, we can use “repmat” and “arrayfun” functions in MATLAB to replicate and concatenate the scaler value “run”, converting it into a cell array containing row vectors of dimension 3x1. Now that all three fields of the structure have same dimensions, we can concatenate them into one matrix by using “vertcat” function.
Below is the complete MATLAB code that solves the task:
% Define the structure array
st(1).run = 1; st(1).x = (0:2)'; st(1).y = 1 * st(1).x;
st(2).run = 2; st(2).x = (5:7)'; st(2).y = 2 * st(2).x;
st(3).run = 3; st(3).x = (0:2)'; st(3).y = 3 * st(3).x;
% Use arrayfun to replicate the run number and concatenate results
runColumn = arrayfun(@(s) repmat(s.run, length(s.x), 1), st, 'UniformOutput', false);
outputMat = [vertcat(runColumn{:}), vertcat(st.x), vertcat(st.y)];
% Display the result
disp('Output Matrix:');
Output Matrix:
disp(outputMat);
1 0 0 1 1 1 1 2 2 2 5 10 2 6 12 2 7 14 3 0 0 3 1 3 3 2 6
Please find attached the documentation of functions used for reference:
I anticipate this will solve the problem.
  1 Kommentar
Voss
Voss am 7 Sep. 2024
Bearbeitet: Voss am 7 Sep. 2024
Another option:
st(1).run = 1; st(1).x = (0:2).'; st(1).y = 1 * st(1).x;
st(2).run = 2; st(2).x = (5:7).'; st(2).y = 2 * st(2).x;
st(3).run = 3; st(3).x = (0:2).'; st(3).y = 3 * st(3).x;
outputMat = [repelem(vertcat(st.run),arrayfun(@(s)numel(s.x),st),1) vertcat(st.x) vertcat(st.y)]
outputMat = 9x3
1 0 0 1 1 1 1 2 2 2 5 10 2 6 12 2 7 14 3 0 0 3 1 3 3 2 6
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by