How would I add an extra dimension to an already existing matrix?

459 views (last 30 days)
So I already have an already existing matrix/array given the command code:
C=randi([-10,10],4,4) %that will give me a 4x4 of ten random integers
Now using the matrix/array that I have from C, how would I add an extra dimension/layer of all 0's that would make the matrix now a 4x4x2. Again, the added dimension/layer would consist of all 0's.
Thank you!

Answers (3)

Walter Roberson
Walter Roberson on 23 Jan 2017
C = randi([-10,10],4,4);
C(end, end, 2) = 0;
Caution: if C is already 3D then the above will have the side effect of setting one of its locations to 0. You can test,
if ndims(C) < 3; C(end, end, 2) = 0; end
  4 Comments
Nolan Makatche
Nolan Makatche on 16 Jul 2020
Can someone explain what C(end,end,2) = 0 means syntax wise?
Why do you need two ends?
When I run this in Matlab it changes the entire array to zeros.
When I change the 2 to a 1 it only changes the last element in the array to zero.
Walter Roberson
Walter Roberson on 16 Jul 2020
>> C = [1 2;3 4]
C =
1 2
3 4
>> C(end,end,2) = 0
C(:,:,1) =
1 2
3 4
C(:,:,2) =
0 0
0 0
Notice that C has gone from being a 2D array to being a 3D array, and the original data is not affected.
C(end,end,2) is the same as C(size(C,1), size(C,2), 2) and effectively names the "bottom corner" of a 3D slice that did not previously exist.

Sign in to comment.


Jorge Mario Guerra González
try this
C=randi([-10,10],4,4);
C(:,:,2)=C;
Which creates a new layer of C matrix to create a 3D array

kevin harianto
kevin harianto on 8 Apr 2022
Edited: kevin harianto on 8 Apr 2022
I am still getting an error whenever I am trying to add more dimensions. at
tempPtCloud = cat(2, tempPtCloud.Location,zeros(10000, size(tempPtCloud.Location,2)));
outputFolder = fullfile(tempdir,"Pandaset");
preTrainedMATFile = fullfile(outputFolder,"trainedSqueezeSegV2PandasetNet.mat");
preTrainedZipFile = fullfile(outputFolder,"trainedSqueezeSegV2PandasetNet.zip");
if ~exist(preTrainedMATFile,"file")
if ~exist(preTrainedZipFile,"file")
disp("Downloading pretrained model (5 MB)...");
component = "lidar";
filename = "data/trainedSqueezeSegV2PandasetNet.zip";
preTrainedZipFile = matlab.internal.examples.downloadSupportFile(component,filename);
end
unzip(preTrainedZipFile,outputFolder);
end
Next install of utilizing the lidar dataSet provided by matlab we shall be utilizing our own.
code
% Load the pretrained network.
outputFolder = fullfile(tempdir,"Pandaset");
load(fullfile(outputFolder,"trainedSqueezeSegV2PandasetNet.mat"),"net");
% Read the point cloud.
%----------------------------Note if we have access to hardware resources
% we would run the function to start loading the files in.
%parfeval(hardwareManipulation);
%-----------------------------
%Next we shall change the outputfolder to face towards the data_2
outputFolderNew=fullfile("data_2");
ptCloud = pcread(fullfile(outputFolderNew,"0000000000.pcd"));
%Due to the ptCloud being a different data type
%With the above information in mind (trying to reach 64 in height)
% In order to match with the Expected file format we shall append additional matrices of 0
% in order to subsitute for the lack of clear resolution.
%These additional values should allow the raw pcd file matric to meet the
%resolutions demand
% Since we are only trying to influence the resolution
% (pandaSet Ideal resolution being 64x1856x3)
% and not the actual
% value's representation we will only be adding in the values
% However because the raw pcd file is single matrix, we shall be adding
% in additional dimensions.
tempPtCloud = ptCloud;
%ptCloud being read only we can not add the third dimension successfully
%-------------from suggestion (error of cat in this line below -inconsistency-)
tempPtCloud = cat(2, tempPtCloud.Location,zeros(10000, size(tempPtCloud.Location,2)));
%third dimension (need new method)
tempPtCloud.Location(end, end, 2) = 0;
%shifting values to the next dimension
B = tempPtCloud.Location;
B(:,3) = tempPtCloud.Location(:,2); %copy second column to move to third
for n = 2: size(ptCloud.Location)
B(:,n)= tempPtCloud.Location(:,n-1);
end
%We can do the above twice to move the 2nd column to the third, and first
%to the second then add in the zeros to the first and replace.
B(:,2) = tempPtCloud.Location(:,1); %copy first column to move to second
for n = 2: size(ptCloud.Location)
B(:,n)= tempPtCloud.Location(:,n-1);
end
%using the location variable B which should contain the 3 Dimensions in its
%rightful format we shall now modify the temporary pointCloud for
%manipulation
tempPtCloud = pointCloud(B(:, 1:3));
%Now that we have added in the additional resolution we
% shall utilize the new ptCloud and treat it as the original
ptCloud = tempPtCloud;
% Convert the point cloud to 5-channel image.
im = helperPointCloudToImage(ptCloud);
% Predict the segmentation result.
predictedResult = semanticseg(im,net);
% Display sematic segmentation result on point cloud
% This should now be displaying our pointClouds that we want to add in and
% analyze for our use case.
helperDisplayLabelOverlaidPointCloud(im,predictedResult);
view([39.2 90.0 60])
title("Semantic Segmentation Result on Point Cloud")
  4 Comments
Walter Roberson
Walter Roberson on 8 Apr 2022
Do I understand correctly that tempPtCloud is 64 x 1856 x 3 ?
If so then what size do you want the result of
tempPtCloud = cat(2, tempPtCloud.Location,zeros(10000));
to be?
kevin harianto
kevin harianto on 8 Apr 2022
Edited: kevin harianto on 8 Apr 2022
I have updated my code and the rror that I am getting now slightly, but the goal was to add in the zeros to compensate for the lack of values in the pointCloud.Location image input as well as the fact that the array is missing a dimension. (to add a new dimension to the pointCloud.Location)
I want the tempPtCloud to have a location min to 64, 1856, 3 double with intensity then being 64, 1856.
However currently it has a location of 123398, 3 single with the intensity being 123398, 1 single as well

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!