How to create a vector by indexing elements within each cell of a 1xN cell array

I have a 1xN cell array (raw_array) and I am trying to create a vector of N values using the same indexing (a,b) within each cell.
I can use this expression to extract a scalar value from a single cell:
raw_array{1,n(x,y)}(a,b)
Here n(x,y) is just a means of indexing the correct cell within the cell array.
Let's say y varies from 1-10, i earlier tried the following:
raw_array{1,n(x,1:10)}(a,b) but clearly this doesn't work (error msg: Expected one output from a curly brace or dot indexing expression, but there were 10 results).
I really want to avoid writing each vector element separately, e.g. [raw_array{1,n(x,1)}(a,b) raw_array{1,n(x,2)}(a,b)...]
Can anybody please give me a hint or suggestion?
Thanks

 Accepted Answer

Where C is your cell array:
a = ..;
b = ..;
F = @(m)m(a,b);
V = cellfun(F,C)

9 Comments

Thanks for the response.
Now I'm getting an error message:
Index in position 1 exceeds array bounds (must not exceed 62758). Note that this is len_min of the first cell within the array. The length of the subsequent cells vary.
Here is my code:
for i = 1:size(n,1)
for j = n_runs(i)
for k = 1:20
for l = 1:len_min(i)
F = @(m)m(l,k);
V = cellfun(F,raw_array);
raw_array_mean{1,i}(l,k) = mean(V(1:j));
end
end
end
end
FYI... the first cell of the 'raw_array_mean' populates fine but the for loop stops on the next increment of len_min
Your code seems to be rather inefficient: give that V does not vary with j, why do you repeatedly generate completely identical V ? Surely it would be much more effiicient to generate V just once for each particular l and k pair and then use that V multiple times for each j.
"Now I'm getting an error message:"
Please post the complete error message. This means all of the red text. The error message contains important, useful information that helps debug code. Hiding this information makes helping you harder.
Please also post the exact sizes of the relevant arrays and the content of any relevant cells.
Probably the best would be if you use dbstop if error and save all of the relevant variables, then upload them here by clicking the paperclip button.
slightly revised code
for i = 1:size(n,1)
for j = 1:20
for k = 1:len_min(i)
F = @(m)m(k,j);
V = cellfun(F,raw_array);
for l = n_runs(i)
raw_array_mean{1,i}(k,j) = mean(V(1:l));
raw_array_error{1,i}(k,j) = std(V(1:l));
end
end
end
end
The error message reads:
Index in position 1 exceeds array bounds (must not exceed 62758).
Error in ADV_Trial_Multiple_DR>@(m)m(k,j) (line 50)
F = @(m)m(k,j);
Error in ADV_Trial_Multiple_DR (line 51)
V = cellfun(F,raw_array);
I've attached the mat file with some variables but unfortunately I cannot add the array data as it's huge (140Mb) but for reference, the raw_array is a 1x11 cell array containing data of varying length/rows (62758, 89004, 73174...) but the other dimension is fixed (20 columns). The data is all doubles.
Can anybody help or is there something else that I can provide which would offer more clarity?
Form the code you have provided it is not clear how n relates to raw_array or len_min. Apparently you are using n and len_min to define the limit to the size of the cell array content, but we have no idea how you defined these values. Too many details missing, which we cannot guess. The error message makes it clear that you are trying to access a non-existent array element, which tells us that whatever you used to define that array size is not correct.
It is possible that you are referring to an older variable, or the wrong dimension, or all number of things.
Please show the code that derives n and len_min from raw_array.
Please show the output of this command:
whos raw_array
for k = 1:numel(raw_array)
size(raw_array{k})
end
Please upload raw_array{index when the error occurs}
whos raw_array
for k = 1:numel(raw_array)
size(raw_array)
end
Name Size Bytes Class Attributes
raw_array 1x11 140651152 cell
I've attached the code and a screenshot of the raw_array contents
Sorry, the code I posted earlier was not correct. Please show the output of this:
for k = 1:numel(raw_array)
size(raw_array{k})
end
No problem, I have reworked the code now so that it makes more sense. Thanks!

Sign in to comment.

More Answers (0)

Products

Release

R2019b

Community Treasure Hunt

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

Start Hunting!