How to pad cell w/ multiple arrays with zeros

Hello,
I have a cell array: myCell = {1,4}, each array inside the cell can be:
ex:
1500X1
2000X1
2048X1
3400X1
I would like to pad all arrays that dont equal max array length(3400) with zeros and plot. Im trying to use cellfun but am having trouble padding.I can find the max using cellfun easy with:
[s,d] = cellfun(@size,myCell);
outputMax = max([s,d]);
But am having trouble figuring out how to use cellfun ,now that I know what the MAX length is of my arrays....I tried padarray but failed miserably.Any ideas?
Thanks

 Accepted Answer

M = max(cellfun(@length, myCell));
Wanted = cellfun(@(x) [x; zeros(M - numel(x), 1)], myCell, 'un', 0)

4 Comments

m j
m j on 28 Jul 2020
Edited: m j on 28 Jul 2020
This worked, thanks. But I tried cell2mat and then plotting and it doesnt plot all of my signals,and if it does plot them there squiggly lines....I noticed that some of my signals can have a max y value of 1000,whereas others can have a max y value of only 6. Im guessing I need to normalize all signals so they can be plotted normally. Any ideas? Ill try finding the max value inside each array and then divide this max by each number inside the array,so max value would then be = 1..
Im also using Matlab 2016,if that has anything to do with cell2mat or plot.
m j
m j on 28 Jul 2020
Edited: m j on 28 Jul 2020
I got it to work and be plotted using code below, but im having a issue setting a plot inside a figure using:
set(hPlot3, 'Xdata',1:M,'Ydata',normWanted(:,:));
Heres code that I got to work and plot without a figure,plot handles:
And apologies,everytime I try to use the 'CODE' command/button on MATLABs website, the website freezes for me....Never had that issue before......
M = max(cellfun(@length, outCell));
padded = cellfun(@(x) [x; zeros(M - numel(x), 1)], outCell, 'un', 0);
%normalize by finding max val in each array
arrayOfmaxes = [];
B = cell2mat(padded);
for i = 1:size(B,2)
%get max of each array
currMax = max(B(:,i));
%store into array
arrayOfmaxes = [currMax arrayOfmaxes];
end
%now divide each array by its own MAX to normalize
normWanted = [];
temp= [];
for j = 1:size(B,2)
%divide by current arrayOfmaxes value and current signal
temp = (B(:,j)/arrayOfmaxes(1,(size(B,2)+1)-j));
normWanted = [temp normWanted];
end
figure();plot(normWanted);
I answered your original question.
You are correct,I accepted answer and asked question elsewhere.Thanks!

Sign in to comment.

More Answers (0)

Categories

Asked:

m j
on 27 Jul 2020

Commented:

m j
on 28 Jul 2020

Community Treasure Hunt

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

Start Hunting!