Simulink, how to get the index z from a 3-D (x,y,z)matrix,then change the (x2,y2) and (x6,y6) according to the index z

2 views (last 30 days)
pic1
The generator will generate 128 bits samples in 10 seconds, the goal is 2 bits as one frame and then change the second frame with the 6-th frame.
I used a buffer to combine 2 bits into 1 frame, and then used a reshape block to change the frame structure. I want to get a (1,2,64) matrix using matlab function block.
Its showed in pic1 that the structure of input to Scope1 is [1*2*64],but I log the data into workspace, it showed that its an 4-D matrix as followed:
I guess the index of frame is generated automatically, cause the Matrix of Scope2 showed in workspace is a 3-D one, but i don't know how to get the dimension z (1-64)

Accepted Answer

Yatharth
Yatharth on 18 Oct 2023
Hi Xiaotian Yu,
I understand that you have a signal of dimension [1x2x64] and while exporting the signal using the "To Workspace" Block in Simulink the Array saved shows up as a 4-D array.
The reason for this is that the 4th dimension is the timestamp so what it really means is at each time stamp your signal value is stored.
To access your z index consider the dimensions as [1x2x64xt] where "t" is the dimension of the "tout" array.
If you want to access your signal value at any particular time you can simply put t = "yourdesiredtimestamp". In your case, I think since the signal is constant at each time stamp the array will have the same value.
You can change the sample time by clicking on the "To Workspace" Block by default it is set to -1.
Alternatively, if you want to separate or extract the "x2y2" and "x6y6" within Simulink itself you can use a function block for the same.
  1. Double-click on the "MATLAB Function block" to open the editor
  2. Write a function that takes a 3-D matrix as an input, extracts the Z index, and modifies the (x, y) pairs. Below is a simple example:
function [out] = fcn(in)
% Extract Z index
z = in(:,:,3);
% Find the index for Z
[idx1, idx2] = find(z == max(z(:))); % This is just an example, you can use your own condition
% Change (x2, y2) and (x6, y6) according to Z index
in(2,2,:) = in(idx1, idx2, :);
in(6,6,:) = in(idx1, idx2, :);
% Output the modified matrix
out = in;
end
3. Connect the input and output ports of the MATLAB Function block to the rest of your model.
I hope this helps.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!