Error using reshape, To RESHAPE the number of elements must not change?

15 visualizzazioni (ultimi 30 giorni)
This code was written by Stephen:
a = [0.75 0.23 -2.86
0.75 0.47 3.87
0.75 0.64 2.78
2.45 0.23 4.56
2.45 0.47 0.89
2.45 0.64 2.74
3.23 0.23 2.10
3.23 0.47 -3.65
3.23 0.64 -5.89]
x = reshape(a(:,1),3,3);
y = reshape(a(:,2),3,3);
z = reshape(a(:,3),3,3);
surf(x,y,z, 'FaceColor','interp')
The code above works, but when I added four more point (each row is a point) the reshape gets an error saying:
"Error using reshape
To RESHAPE the number of elements must not change.
Error in Code_Version_1 (line 16)
x = reshape(a(:,1),3,3); "
Code below gets the error.
a = [0.75 0.23 -2.86
0.75 0.47 3.87
0.75 0.64 2.78
2.45 0.23 4.56
2.45 0.47 0.89
2.45 0.64 2.74
3.23 0.23 2.10
3.23 0.47 -3.65
3.23 0.64 -5.89
0 0 0
4 0 0
4 1 0
0 1 0]
x = reshape(a(:,1),3,3);
y = reshape(a(:,2),3,3);
z = reshape(a(:,3),3,3);
surf(x,y,z, 'FaceColor','interp')
I believe it is because the dimensions of the matrix is changing(from 9x3 to 13x3), but I do not know how to alter the code in order to accommodate for the change in the new dimensions of the matrix.

Risposta accettata

dpb
dpb il 9 Giu 2015
Modificato: dpb il 9 Giu 2015
Well, since 13 is prime, you're stuck, there is no factor of 13 other than 1 and itself.
By symmetry to the initial, it looks like you need multiples of three and I note that for the initial data the first column value is repeated that many times--altho I've not tried to dig into what Stephen was doing here, I'd venture that is probably an unstated assumption for the method to work.
But, the general syntax with a proper set of input data (namely length divisible by N) would be
reshape(x,N,[])
where the [] tells Matlab to use length(x)/N as the second dimension.
Now whether that works for the intended purpose here is another question altho I would venture it would if the additional points follow the same pattern as the initial data set.
  2 Commenti
kevin harianto
kevin harianto il 6 Apr 2022
I am also trying to reshape an array to 64, 1856, 3 however it i am getting the same error. I have also tried permut but to no luck.
classdef LidarSemanticSegmentation < lidar.labeler.AutomationAlgorithm
% LidarSemanticSegmentation Automation algorithm performs semantic
% segmentation in the point cloud.
% LidarSemanticSegmentation is an automation algorithm for segmenting
% a point cloud using SqueezeSegV2 semantic segmentation network
% which is trained on Pandaset data set.
%
% See also lidarLabeler, groundTruthLabeler
% lidar.labeler.AutomationAlgorithm.
% Copyright 2021 The MathWorks, Inc.
% ----------------------------------------------------------------------
% Step 1: Define the required properties describing the algorithm. This
% includes Name, Description, and UserDirections.
properties(Constant)
% Name Algorithm Name
% Character vector specifying the name of the algorithm.
Name = 'Lidar Semantic Segmentation';
% Description Algorithm Description
% Character vector specifying the short description of the algorithm.
Description = 'Segment the point cloud using SqueezeSegV2 network.';
% UserDirections Algorithm Usage Directions
% Cell array of character vectors specifying directions for
% algorithm users to follow to use the algorithm.
UserDirections = {['ROI Label Definition Selection: select one of ' ...
'the ROI definitions to be labeled'], ...
'Run: Press RUN to run the automation algorithm. ', ...
['Review and Modify: Review automated labels over the interval ', ...
'using playback controls. Modify/delete/add ROIs that were not ' ...
'satisfactorily automated at this stage. If the results are ' ...
'satisfactory, click Accept to accept the automated labels.'], ...
['Accept/Cancel: If the results of automation are satisfactory, ' ...
'click Accept to accept all automated labels and return to ' ...
'manual labeling. If the results of automation are not ' ...
'satisfactory, click Cancel to return to manual labeling ' ...
'without saving the automated labels.']};
end
% ---------------------------------------------------------------------
% Step 2: Define properties you want to use during the algorithm
% execution.
properties
% AllCategories
% AllCategories holds the default 'unlabelled', 'Vegetation',
% 'Ground', 'Road', 'RoadMarkings', 'SideWalk', 'Car', 'Truck',
% 'OtherVehicle', 'Pedestrian', 'RoadBarriers', 'Signs',
% 'Buildings' categorical types.
AllCategories = {'unlabelled'};
% PretrainedNetwork
% PretrainedNetwork saves the pretrained SqueezeSegV2 network.
PretrainedNetwork
end
%----------------------------------------------------------------------
% Note: this method needs to be included for lidarLabeler app to
% recognize it as using pointcloud
methods (Static)
% This method is static to allow the apps to call it and check the
% signal type before instantiation. When users refresh the
% algorithm list, we can quickly check and discard algorithms for
% any signal that is not support in a given app.
function isValid = checkSignalType(signalType)
isValid = (signalType == vision.labeler.loading.SignalType.PointCloud);
end
end
%----------------------------------------------------------------------
% Step 3: Define methods used for setting up the algorithm.
methods
function isValid = checkLabelDefinition(algObj, labelDef)
% Only Voxel ROI label definitions are valid for the Lidar
% semantic segmentation algorithm.
isValid = labelDef.Type == lidarLabelType.Voxel;
if isValid
algObj.AllCategories{end+1} = labelDef.Name;
end
end
function isReady = checkSetup(algObj)
% Is there one selected ROI Label definition to automate.
isReady = ~isempty(algObj.SelectedLabelDefinitions);
end
end
%----------------------------------------------------------------------
% Step 4: Specify algorithm execution. This controls what happens when
% the user presses RUN. Algorithm execution proceeds by first
% executing initialize on the first frame, followed by run on
% every frame, and terminate on the last frame.
methods
function initialize(algObj,~)
% Load the pretrained SqueezeSegV2 semantic segmentation network.
outputFolder = fullfile(tempdir, 'Pandaset');
pretrainedSqueezeSeg = load(fullfile(outputFolder,'trainedSqueezeSegV2PandasetNet.mat'));
% Store the network in the 'PretrainedNetwork' property of this object.
algObj.PretrainedNetwork = pretrainedSqueezeSeg.net;
end
function autoLabels = run(algObj, pointCloud)
% Setup categorical matrix with categories including
% 'Vegetation', 'Ground', 'Road', 'RoadMarkings', 'SideWalk',
% 'Car', 'Truck', 'OtherVehicle', 'Pedestrian', 'RoadBarriers',
% and 'Signs'.
autoLabels = categorical(zeros(size(pointCloud.Location,1), size(pointCloud.Location,2)), ...
0:12,algObj.AllCategories);
%A = zeros(10000,10000);
%filling in the minimum required resolution
% to meet the neural network's specification.
%(first iteration failed) pointCloud.Location = zeros(65,1856,5);
%Due to an error we must append the various point cloud data
%first.
%next we can add in the ptCloud locations
% Location(:,:,1) = pointCloud.Location;
% Location = zeros(65,1856,5);
% adding the additional elements to the array.
appendingArray = zeros(64,1856,3);
Location = [pointCloud];
%We now have to convert the location 1D array to 3D array
% permute(reshape(TheList, 300, 300, 400), [2 1 3]).
% reshape(Location, 64, 1856, 3);
Location = permute(reshape(Location, 3, 1856, 64), [3 2 1]);
Location =[Location, appendingArray];
%This will also be applied to the pointCloud Intensity levels
% as these are also analyzed by the machine learning algorithm.
%(Pushed aside for later modifications) pointCloud.Intensity = zeros(64,1865);
pointCloud=Location;
% Convert the input point cloud to five channel image.
I = helperPointCloudToImage(pointCloud);
% Predict the segmentation result.
predictedResult = semanticseg(I, algObj.PretrainedNetwork);
autoLabels(:) = predictedResult;
%using this area we would be able to continuously update the latest file on
% sending the output towards the CAN Network or atleast ensure that the
% item is obtainable
% This area would work the best.
%first we must
end
end
end
function helperDisplayLabelOverlaidPointCloud(I,predictedResult)
% helperDisplayLabelOverlaidPointCloud Overlay labels over point cloud object.
% helperDisplayLabelOverlaidPointCloud(I,predictedResult)
% displays the overlaid pointCloud object. I is the 5 channels organized
% input image. predictedResult contains pixel labels.
ptCloud = pointCloud(I(:,:,1:3),Intensity = I(:,:,4));
cmap = helperPandasetColorMap;
B = ...
labeloverlay(uint8(ptCloud.Intensity),predictedResult,Colormap = cmap,Transparency = 0.4);
pc = pointCloud(ptCloud.Location,Color = B);
ax = pcshow(pc);
set(ax,XLim = [-70 70],YLim = [-70 70])
zoom(ax,3.5)
end
function cmap = helperPandasetColorMap
cmap = [[30 30 30]; % Unlabeled
[0 255 0]; % Vegetation
[255 150 255]; % Ground
[237 117 32]; % Road
[255 0 0]; % Road Markings
[90 30 150]; % Sidewalk
[255 255 30]; % Car
[245 150 100]; % Truck
[150 60 30]; % Other Vehicle
[255 255 0]; % Pedestrian
[0 200 255]; % Road Barriers
[170 100 150]; % Signs
[255 0 255]]; % Building
cmap = cmap./255;
end
function image = helperPointCloudToImage(ptcloud)
% helperPointCloudToImage converts the point cloud to 5 channel image
image = ptcloud.Location;
image(:,:,5) = ptcloud.Intensity;
rangeData = iComputeRangeData(image(:,:,1),image(:,:,2),image(:,:,3));
image(:,:,4) = rangeData;
index = isnan(image);
image(index) = 0;
end
function rangeData = iComputeRangeData(xChannel,yChannel,zChannel)
rangeData = sqrt(xChannel.*xChannel+yChannel.*yChannel+zChannel.*zChannel);
end
dpb
dpb il 9 Apr 2022
Modificato: dpb il 9 Apr 2022
Far too much code and none of it of any real help -- but, if the reshape operation fails, that implies the input array isn't of conforming size. The (one and only) solution is to fix that problem.

Accedi per commentare.

Più risposte (0)

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by