Extracting elements from matrix using strings subscript error - excel

Hi I have a massive excel table and I want to extract values for specific compounds. Matrix repeats itself in 72 blocks, where vehicle-class is repeated with different road gradients and compounds and driving conditions.
cb=[Vehicleclass_raw,Gradient_raw,Component_raw];
size cb= 119952x3 cell
Vehicleclass_airquis is string with 37 elements
so I thought I sort it according to criteria gradient vehicle class and component and this is the code blow. it works for the first 30 elements and then it crashes and error message is Subscripted assignment dimension mismatch. outval_aq(i,:) = (rows). I cant figure out what the error is. Thanks for the help Matthias
outval_aq = ones(37,72)*119953;
for i=1:37
rows = find(strcmp(cb(:,1),Vehclass_airquis(i)) & strcmp(cb(:,2),'0%') & strcmp(cb(:,3),'NOx'));
if ~isempty(rows)
outval_aq(i,:) = (rows)
end
end

In a Matrix each row has the same number of elements. In your case you initialized outval_aq to be a 37x72 matrix but rows has only 60 elements. Solutions could look like:
%pad rows with nans to make it the required size:
rows(end+1:72)=nan
outval_aq(i,:) = (rows)
.
%write only the first 60 elements to the matrix, leave the remaining untouched:
outval_aq(i,1:numel(rows)) = (rows)
Or you could change outval_aq to be a cell array which contains vectors.

Related

[Excel][Math] Formula to generate numbers between two limits

I am trying to fill a column in Excel with values that are generated randomly but the sum of all the values total to the end.
Example:
Starting value = 320
Final value = 350
Need to generate 2 more values which have random difference between them but total to 350 at the end, as in: 2nd val = 12, 3rd val = 18.
The script/formula should generate different values when run next (for another table etc.). It may generate, for the same starting and final values, 15 and 15 or 8 and 22 for the 2nd and 3rd values respectively etc.
Basically what the formula should do is: Find the difference between the starting and final values then randomly add a number to create the 2nd entry. Now the third entry should follow the same pattern but the value generated should end up totaling to the final.
The example is only for 2 values but I'm working on tables ranging from 15-30+ values.
I don't know if Excel can do it, or if there's a mathematical formula that will work here.
Thanks in advance for all the help!
You need something like this:
That gives:
The trick is to generate a list of uniform random numbers between 0 and 1 and then scale those numbers up the total that you're looking for.

How to query a matrix for multiple values and receive value

I am a bit lost with Excel. I have two values
Length
Weigth
I am looking for a solution for the problem below:
Take length and weight from the input values, find the next upper value for both in the matrix and receive the value from the value column. And if there is no upper value for my input, i want to have a fallback and always use teh value 1.0.
How to solve this?
I have a matrix which has these values inside:
You could try:
=IFERROR(INDEX(SORT(SORT(FILTER(MATRIX, (LENGTHS>length)*(WEIGHTS>weight)),1,1),2,1),1,3),1)
, where MATRIX, LENGTHS, and WEIGHTS refer to the data on the right, whilst length and weight refer to the cells containing 80 and 450.
Just seen you said H6 was what I referred to as length - if so:
=IFERROR(INDEX(SORT(SORT(FILTER(K6:M11, (K6:K11>H6)*(L6:L11>H7)),1,1),2,1),1,3),1)
in H8.
You can use XMATCH to get the next value as follow:
=LET(f, FILTER(D2:F7, D2:D7>B1),
IFNA(INDEX(INDEX(f,,3), XMATCH(B2, INDEX(f,,2),1)), 1))
If you want to treat the error when the length condition is not satisfied, you can modify it as follows:
=LET(f, FILTER(D2:F7, D2:D7>B1, ""), IF(#f="", "No match for length condition",
IFNA(INDEX(INDEX(f,,3),XMATCH(B2, INDEX(f,,2),1)), 1)))

Combine two matrices row by row

I have two matrices of the size [18,28,28,5,5] and I want to merge them and get a matrix of the size [18,280,140] in the following sense. I take each square 5x5 from the first matrix and put a 5x5 square from the second matrix below it. That way I have 280 (28x5x2) rows and 140 columns (28x5).
Is there a fast way to do it? I've tried with for loops over 28 but it takes a lot of time.
Code example:
for row in range(28):
for col in range(28):
C[i,:,row*10:(row+1)*10,col*5:(col+1)*5] = torch.cat((torch.reshape(A[:,row,col,:],[18,5,5]),torch.reshape(B[:,row,col,:],[18,5,5])),dim=1)
I'm pretty sure that I can use unsqueeze instead of reshape but that's how I've done it.
Thanks!

Determine Size Of Array of Equal Items Excel

I have an array that looks like this
11100100110
essentially, an array of fixed size with each item being a 1 or 0 with the last item always equal to 0.
Consider each set of consecutive 1's to be a "bucket". I'd like a formula to determine the size of each bucket. So the output of this formula for the above sequence should be
312
as an array. Ideally this works in both excel and google sheets.
If you are interested this is the result of a list of stars and bars configurations where the 0's in my sequence represent bars and the 1's represent stars (the final value is a dummy 0 to make things easier to work with). I want the size of each non-empty bucket in a given configuration of stars and bars.
Thanks, in advance.
You could also use the standard method with Frequency which will work with Excel 365 and GS:
=FILTER(FREQUENCY(IF(A1:A11=1,ROW(A1:A11)),IF(A1:A11=0,ROW(A1:A11))),FREQUENCY(IF(A1:A11=1,ROW(A1:A11)),IF(A1:A11=0,ROW(A1:A11))))
try:
=INDEX((JOIN(, LEN(SPLIT(A1, 0)))))
update:
=INDEX(IFERROR(1/(1/SUBSTITUTE(FLATTEN(QUERY(TRANSPOSE(IFERROR(1/(1/
LEN(SPLIT(SUBSTITUTE(FLATTEN(QUERY(
TRANSPOSE(A1:K),, 9^9)), " ", ), 0))))),, 9^9)), " ", ))))
Assuming A2:A9 contains the data,
=ARRAYFORMULA(QUERY(FREQUENCY(IF(A2:A9,ROW(A2:A9)),IF(NOT(A2:A9),ROW(A2:A9))),"where Col1>0",))
FREQUENCY(data,classes) to get the frequency of data in classes
Make sequence of row numbers as data, if 1
Make sequence of row numbers as classes, if not 1
QUERY to get rid of zeros

Finding but not removing duplicates in Matlab

I have a big array in Matlab like this:
A =
{1x5 cell}
{1x7 cell}
{1x27 cell}
{1x11 cell}
...
where the cells look like this:
C{1}
ans =
'apple' 'banana' 'kiwi' 'orange'
I want to find where in A find cells containing double information, like:
C{27}
ans =
'turtle' 'kiwi' 'fox' 'badger'
I.e. here I want to see if C(1) and C(27) has a duplicate word 'kiwi'.
So I can manually look at them and decide where I should remove the duplicate where I see fit.
Sorry I'm not going to provide a coded solution, more the process I'd use so that you can start coding, if you then have any specific problems fell free to post an question
I would use nchoosek to generate an array of all the permutations of the cell array C so
nCells = length(C);
nPerms = nchoosek(1:nCells,2);
You can then loop over all the permutations using intersect to see if there are common strings.
result(i) = intersect(C{nPerms(i,1)},C{nPerms(i,2)});
This will give you an array listing all common strings and with the nPerms array you'll have the two rows with the common string. However if you try to run this it will fail as intersect likes to have the same number of element in each cell array.
So I'd create a temporary cell array padded out with blank cells so that each element in C is the same length, prior to the loop.
This will calculate the longest cell in the array C by calculating the number of elements (#numel) in each cell, followed by calculating the maximum.
cSize = cellfun(#numel,C);
maxSize = max(cSize);
We can then define a function to pad out blank cells
fcn = #(x) [x cell(1,maxSize - numel(x))];
paddedC = cellfun(fcn,C,'UniformOutput',false);
This should give you a cell array with same number of elements in each cell. You can then use this cell array in your loop testing each permutation.
No doubt someone will turn up with a one line cellfun solution but I hope that this is enough to get you started.

Resources