Populating matrix in for loop generates error
显示 更早的评论
I have two prealocated matrices I'm trying to populate using a for loop, but I keep getting this error:
"Unable to perform assignment because the left and right sides have a different number of elements."
here is the code:
% Load the neuron table
load neuronTable.mat;
ctrl_neuronTable = neuronTable(strcmp(neuronTable.condition,'control'),:);%just the control FOVs
ipRGC_neuronTable = ctrl_neuronTable(strcmp(ctrl_neuronTable.iprgcID,"yes"),:); %just the ipRGCs
RGC_neuronTable = ctrl_neuronTable(strcmp(ctrl_neuronTable.iprgcID,"none"),:); %just the RGCs
% Two useful vars here for the rest of the code
listFOVs = unique(ctrl_neuronTable.movie_name); %List of unique FOVs
numFOVs = length(listFOVs); %total num of FOVs
total_ipRGCs = length(ipRGC_neuronTable.neuronNum);
total_RGCs = length(RGC_neuronTable.neuronNum);
%Initializing some vars that we will fill in the for loop
ctrl_cellPartRGC = nan(total_RGCs,1);
ctrl_cellPartIPRGC = nan(total_ipRGCs,1);
ctrl_meanAmpRGC = nan(total_RGCs,1);
ctrl_meanAmpIPRGC = nan(total_ipRGCs,1);
allIPRGCids = [];
allMaxCellAmps = [];
numEvents = neuronTable.numWaves;
for i = 1:numFOVs
% for i = 1:1 %This line for testing code
%generate tempTable of current FOV
tempTable = ctrl_neuronTable(strcmp(ctrl_neuronTable.movie_name,listFOVs(i)),:);
numWaves = tempTable.numWaves(i); %total num of waves
numCells = size(tempTable,1);
%Calc amp of cells when they participate in waves
indWhenWavesHappen = tempTable.cellPart;
indWhenWavesHappen(isnan(indWhenWavesHappen)) = 0;
cellAmps = tempTable.maxCellAmp;
cellAmps(isnan(cellAmps)) = 0;
ampOfCellsDuringWaves = indWhenWavesHappen.*cellAmps;
[ii,~,v] = find(ampOfCellsDuringWaves); %These two lines calc mean without counting zeros
meanCellAmps = accumarray(ii,v,[],@mean);
tempTable.meanCellAmps = meanCellAmps;
allIPRGCids = [allIPRGCids;tempTable.iprgcID];
allMaxCellAmps = [allMaxCellAmps;meanCellAmps];
% extract the ipRGC and rgc info to intialize variable with ipRGC and rgc avg part per FOV
ctrlTable = tempTable(strcmp(tempTable.iprgcID,'none'),:);
ipRGCTable = tempTable(strcmp(tempTable.iprgcID,'yes'),:);
%Calc percent wave part of each cell type and export to vars we initialized
ctrl_cellPartRGC(i) = sum(ctrlTable.cellPart,2, 'omitnan')./numWaves(i);
ctrl_cellPartIPRGC(i) = sum(ipRGCTable.cellPart,2, 'omitnan')./numWaves(i);
ctrl_meanAmpRGC(i) = mean(ctrlTable.meanCellAmps);
ctrl_meanAmpIPRGC(i) = mean(ipRGCTable.meanCellAmps);
end
The two variables are
ctrl_cellPartRGC ( size = 5655) and ctrl_cellPartIPRGC (size = 1175)
as the loop iterates through the different numFOVs, the right side of the line populating the two matrices changes. this is expected, as tI want the right side to be saved into the matrices after each iteration. I'm not sure what I need to change. Any ideas?
2 个评论
Stephen23
2021-5-25
@Christiane Voufo: please show us the complete error message. This means all of the red text. Do not edit it.
Christiane Voufo
2021-5-25
回答(1 个)
Jan
2021-5-25
Use the debugger to examine the problem:
dbstop if error
Then run the code again. When Matlab stops at the error, check the sizes of the variables:
% ctrl_cellPartRGC(i) = sum(ctrlTable.cellPart,2, 'omitnan')./numWaves(i);
size(i)
size(ctrlTable.cellPart)
size(sum(ctrlTable.cellPart,2, 'omitnan'))
size(numWaves(i))
I assume in this line:
ctrlTable = tempTable(strcmp(tempTable.iprgcID,'none'),:)
a 2D matrix is replied instead of the expected row vector.
类别
在 帮助中心 和 File Exchange 中查找有关 Debugging and Improving Code 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!