Matlab, Random Cell Array - arrays

I have a cell array Q, wich contains questions. And a Logical vector containing 1/0 as true / false and in same order as Q like this:
Q = {'A ball is squared: ' 'My computer is slow: ' 'A triangle has 3 corners: '};
A = {0 1 1};
I would then make a Q_random, containing the questions from Q but in randomly order and a A_random contaning the logical numbers which respond to the Q_random. I've come up with this code, but i not sure that this is the best way to do it.
Could I use another method, which is more simple and effective ?
Q = {'A ball is squared: ' 'My computer is slow: ' 'A triangle has 3 corners: '};
A = {0 1 1};
Q_random = cell(1,numel(Q));
A_random = cell(1,numel(Q));
i = 1;
while (i <= numel(Q));
random_number = randi(numel(Q));
if isempty(Q_random{random_number});
Q_random(random_number) = Q(i);
A_random(random_number) = A(i);
i = i + 1;
else
end

I would use randperm to generate randomly ordered indexes
rand_ind=randperm(length(Q));
and then use the random indexes to generate the randomly permuted cell arrays
Q_random=Q(rand_ind);
A_random=A(rand_ind);
This answer to a previous related question may also be worth looking at.

Related

Matlab: Numerical array index into a string array (without loops)

I'm doing a set of problems from the MATLAB's introductory course at MIT OCW. You can see it here, it's problem number 9, part g.iii.
I have one matrix with the final grades of a course, all of them range from 1 to 5. And I have another array with only letters from 'F' to 'A' (in a 'decreasing' order).
I know how to change elements in a matrix, I suppose I could do something like this for each number:
totalGrades(find(totalGrades==1)) = 'F';
totalGrades(find(totalGrades==2)) = 'E';
totalGrades(find(totalGrades==3)) = 'C';
totalGrades(find(totalGrades==4)) = 'B';
totalGrades(find(totalGrades==5)) = 'A';
But then, what's the purpose of creating the string array "letters"?
I thought about using a loop, but we're supposed to solve the problem without one at that point of the course.
Is there a way? I'll be glad to know. Here's my code for the whole problem, but I got stuck in that last question.
load('classGrades.mat');
disp(namesAndGrades(1:5,1:8));
grades = namesAndGrades(1:15,2:size(namesAndGrades,2));
mean(grades);
meanGrades = nanmean(grades);
meanMatrix = ones(15,1)*meanGrades;
curvedGrades = 3.5*(grades./meanMatrix);
% Verifying
nanmean(curvedGrades)
mean(curvedGrades)
curvedGrades(curvedGrades>=5) = 5;
totalGrades = nanmean(curvedGrades,2);
letters = 'FDCBA';
Thanks a lot!
Try:
letters=['F','D','C','B','A'];
tg = [1 2 1 3 3 1];
letters(tg)
Result:
ans = FDFCCF
This works even when tg (total grade) is a matrix:
letters=['F','D','C','B','A'];
tg = [1 2 1 ; 3 3 1];
result = letters(tg);
result
result =
FDF
CCF
Edit (brief explanation):
It is easy to understand that when you do letters(2) you get the second element of letters (D).
But you can also select several elements from letters by giving it an array: letters([1 2]) will return the first and second elements (FD).
So, letters(indexesArray) will result in a new array that has the same length of indexesArray. But, this array has to contain numbers from 1 to the length of letters (or an error will pop up).

Gnuplot: Nested “plot” iteration (“plot for”) with dependent loop indices

I have recently attempted to concisely draw several graphs in a plot using gnuplot and the plot for ... syntax. In this case, I needed nested loops because I wanted to pass something like the following index combinations (simplified here) to the plot expression:
i = 0, j = 0
i = 1, j = 0
i = 1, j = 1
i = 2, j = 0
i = 2, j = 1
i = 2, j = 2
and so on.
So i loops from 0 to some upper limit N and for each iteration of i, j loops from 0 to i (so i <= j). I tried doing this with the following:
# f(i, j, x) = ...
N = 5
plot for [i=0:N] for [j=0:i] f(i, j, x) title sprintf('j = %d', j)
but this only gives five iterations with j = 0 every time (as shown by the title). So it seems that gnuplot only evaluates the for expressions once, fixing i = 0 at the beginning and not re-evaluating to keep up with changing i values. Something like this has already been hinted at in this answer (“in the plot for ... structure the second index cannot depend on the first one.”).
Is there a simple way to do what I want in gnuplot (i.e. use the combinations of indices given above with some kind of loop)? There is the do for { ... } structure since gnuplot 4.6, but that requires individual statements in its body, so it can’t be used to assemble a single plot statement. I suppose one could use multiplot to get around this, but I’d like to avoid multiplot if possible because it makes things more complicated than seems necessary.
I took your problem personally. For your specific problem you can use a mathematical trick. Remap your indices (i,j) to a single index k, such that
(0,0) -> (0)
(1,0) -> (1)
(1,1) -> (2)
(2,0) -> (3)
...
It can be shown that the relation between i and j and k is
k = i*(i+1)/2 + j
which can be inverted with a bit of algebra
i(k)=floor((sqrt(1+8.*k)-1.)/2.)
j(k)=k-i(k)*(i(k)+1)/2
Now, you can use a single index k in your loop
N = 5
kmax = N*(N+1)/2 + N
plot for [k=0:kmax] f(i(k), j(k), x) title sprintf('j = %d', j(k))

Sort array elements by the frequency of its elements

Is it possible in matlab/octave to use the sort function to sort an array based on the relative frequency of their elements?
For example the array
m= [4,4,4,10,10,10,4,4,5]
should result in this array:
[5,10,10,10,4,4,4,4,4]
5 is the less frequent element and is on the top while 4 is the most frequent and it's on bottom.
Should one use the indices provided by histcount?
The following code first calculates how often each element occurs and then uses runLengthDecode to expand the unique elements.
m = [4,4,4,10,10,10,4,4,5];
u_m = unique(m);
elem_count = histc(m,u_m);
[elem_count, idx] = sort(elem_count);
m_sorted = runLengthDecode(elem_count, u_m(idx));
The definition of runLengthDecode is copied from this answer:
For MATLAB R2015a+:
function V = runLengthDecode(runLengths, values)
if nargin<2
values = 1:numel(runLengths);
end
V = repelem(values, runLengths);
end
For versions before R2015a:
function V = runLengthDecode(runLengths, values)
%// Actual computation using column vectors
V = cumsum(accumarray(cumsum([1; runLengths(:)]), 1));
V = V(1:end-1);
%// In case of second argument
if nargin>1
V = reshape(values(V),[],1);
end
%// If original was a row vector, transpose
if size(runLengths,2)>1
V = V.'; %'
end
end
One way would be to use accumarray to find the count of each number (I suspect you can use histcounts(m,max(m))) but then you have to clear all the 0s).
m = [4,4,4,10,10,10,4,4,5];
[~,~,subs]=unique(m);
freq = accumarray(subs,subs,[],#numel);
[~,i2] = sort(freq(subs),'descend');
m(i2)
By combinging my approach with that of m.s. you can get a simpler solution:
m = [4,4,4,10,10,10,4,4,5];
[U,~,i1]=unique(m);
freq= histc(m,U);
[~,i2] = sort(freq(i1),'descend');
m(i2)
You could count the number of repetitions with bsxfun, sort that, and apply that sorting to m:
[~, ind] = sort(sum(bsxfun(#eq,m,m.')));
result = m(ind);

Count items in one cell array in another cell array matlab

I have 2 cell arrays which are "celldata" and "data" . Both of them store strings inside. Now I would like to check each element in "celldata" whether in "data" or not? For example, celldata = {'AB'; 'BE'; 'BC'} and data={'ABCD' 'BCDE' 'ACBE' 'ADEBC '}. I would like the expected output will be s=3 and v= 1 for AB, s=2 and v=2 for BE, s=2 and v=2 for BC, because I just need to count the sequence of the string in 'celldata'
The code I wrote is shown below. Any help would be certainly appreciated.
My code:
s=0; support counter
v=0; violate counter
SV=[]; % array to store the support
VV=[]; % array to store the violate
pairs = ['AB'; 'BE'; 'BC']
%celldata = cellstr(pairs)
celldata = {'AB'; 'BE'; 'BC'}
data={'ABCD' 'BCDE' 'ACBE' 'ADEBC '} % 3 AB, 2 BE, 2 BC
for jj=1:length(data)
for kk=1:length(celldata)
res = regexp( data(jj),celldata(kk) )
m = cell2mat(res);
e=isempty(m) % check res array is empty or not
if e == 0
s = s + 1;
SV(jj)=s;
v=v;
else
s=s;
v= v+1;
VV(jj)=v;
end
end
end
If I am understanding your variables correctly, s is the number of cells which the substring AB, AE and, BC does not appear and v is the number of times it does. If this is accurate then
v = cellfun(#(x) length(cell2mat(strfind(data, x))), celldata);
s = numel(data) - v;
gives
v = [1;1;3];
s = [3;3;1];

How to calculate the weighted average over a cell-array of arrays?

In generalisation of my previous question, how can a weighted average over cell elements (that are and shall remain arrays themselves) be performed?
I'd start by modifying gnovice's answer like this:
dim = ndims(c{1}); %# Get the number of dimensions for your arrays
M = cat(dim+1,c{:}); %# Convert to a (dim+1)-dimensional matrix
meanArray = sum(M.*weigth,dim+1)./sum(weigth,dim+1); %# Get the weighted mean across arrays
And before that make sure weight has the correct shape. The three cases that I think need to be taken care of are
weight = 1 (or any constant) => return the usual mean value
numel(weight) == length(c) => weight is per cell-element c{n} (but equal for each array element for fixed n)
numel(weight) == numel(cell2mat(c)) => each array-element has its own weight...
Case one is easy, and case 3 unlikely to happen so at the moment I'm interested in case 2: How can I transform weight into a array such that M.*weight has the correct dimensions in the sum above? Of course an answer that shows another way to obtain a weighted averaged is appreciated as well.
edit In fact, case 3 is even more trivial(what a tautology, apologies) than case 1 if weight has the same structure as c.
Here's an example of what I mean for case 2:
c = { [1 2 3; 1 2 3], [4 8 3; 4 2 6] };
weight = [ 2, 1 ];
should return
meanArray = [ 2 4 3; 2 2 4 ]
(e.g. for the first element (2*1 + 1*4)/(2+1) = 2)
After familiarizing myself with REPMAT, now here's my solution:
function meanArray = cellMean(c, weight)
% meanArray = cellMean(c, [weight=1])
% mean over the elements of a cell c, keeping matrix structures of cell
% elements etc. Use weight if given.
% based on http://stackoverflow.com/q/5197692/321973, courtesy of gnovice
% (http://stackoverflow.com/users/52738/gnovice)
% extended to weighted averaging by Tobias Kienzler
% (see also http://stackoverflow.com/q/5231406/321973)
dim = ndims(c{1}); %# Get the number of dimensions for your arrays
if ~exist('weight', 'var') || isempty(weight); weight = 1; end;
eins = ones(size(c{1})); % that is german for "one", creative, I know...
if ~iscell(weight)
% ignore length if all elements are equal, this is case 1
if isequal(weight./max(weight(:)), ones(size(weight)))
weight = repmat(eins, [size(eins)>0 length(c)]);
elseif isequal(numel(weight), length(c)) % case 2: per cell-array weigth
weight = repmat(shiftdim(weight, -3), [size(eins) 1]);
else
error(['Weird weight dimensions: ' num2str(size(weight))]);
end
else % case 3, insert some dimension check here if you want
weight = cat(dim+1,weight{:});
end;
M = cat(dim+1,c{:}); %# Convert to a (dim+1)-dimensional matrix
sumc = sum(M.*weight,dim+1);
sumw = sum(weight,dim+1);
meanArray = sumc./sumw; %# Get the weighted mean across arrays

Resources