How do I solve, Two string arrays J=(nx1) and K=(mx1), with same values, if some of the values are missing in J then I need to create a new array for those missing values L=(ix1) ; for example:
J={Two_Headlights
one_engine
four_wheels
two_seats
two_seatbelts}
K={Two_Headlights
one_engine
one_gear
one_break
one_clutch
four_wheels
two_seats
two_seatbelts}
then I would like to create a new array for those missing values in J;
L={one_gear
one_break
one_clutch}
I have tried using the for loop by using setdiff and aswell as using strcmp, but I dont know where I am going wrong, I am not able to get the result.
I guess you missed putting the single quotation for the strings when writing your question.
The setdiff(A,B) function will return the data in A that is not in B. So your first argument must be K.
J={'Two_Headlights','one_engine','four_wheels','two_seats','two_seatbelts'};
K={'Two_Headlights','one_engine','one_gear','one_break','one_clutch','four_wheels','two_seats','two_seatbelts'};
L = setdiff(K,J);
Related
I am writing a method which accepts a two dimensional array of doubles and an int row number as parameters and returns the highest value of the elements in the given row.
it looks like this:
function getHighestInRow(A, i)
return(maximum(A[:i,:]))
end
the issue i am having is when i slice the array with
A[:i,:]
I get an argument error because the :i makes i get treated differently.
the code works in the other direction with
A[:,i,:]
Is there a way to escape the colon? so that i gets treated as a variable after a colon?
You're doing something strange with the colon. In this case you're using the symbol :i not the value of i. Just getHighestInRow(A,i) = maximum(A[i,:]) should work.
Edit: As Dan Getz said in the comment on the question, getHighestInRow(A,i) = maximum(#view A[i,:]) is more efficient, though, as the slicing will allocate a temporary unnecessary array.
I have an array of a given dimension. I want to swap two given elements of the array. I tried to swap the two with the help of a temporary variable, but the result was bogus and confusing. Can someone suggest some other way of doing so?
Also , it would be really helpful if someone told me why my current program is not working.
Swapping using a temporary variable is the correct way.
There's no way to figure out what error you did to make it fail, since you didn't post any code.
In general for an array of type T we can swap the elements at positions i and j like so:
T x[N];
const T tmp = x[i];
x[i] = x[j];
x[j] = tmp;
This works for any type T, including structs.
I have a dozens of arrays with different array names and I would like to do some mathematical calculations in to for loop array by array. I srucked in calling these array into for loop. Is there anybody can help me with this problem? text1 array contains array names. My "s" struct has all these arrays with the same name content of text1 array.
text1=['s.CustomerArray.DistanceDriven','s.CustomerArray.TimeDriven'];
for i=1:3
parameter=str2num(text1(i));
k=size(parameter,2);
a=100;
y=zeros(a,k);
end
After this part my some other calculations should start using "parameter"
Regards,
Eren
I think you are doing several things wrong, here are some pointers.
Rather than listing them manually, consider looping over the fieldnames which can be obtained automatically.
If you are looping over strings, make sure to use a cell array with , rather than a matrix.
If you have a constant, declare it outside the loop, rather than inside the loop. This won't break the code but just makes for obsolete evaluations.
If you want to store results obtained inside a loop, make sure to add an index to the variable that you loop over.
That being said, here is a guess at what you are trying to do:
f = fieldnames(s.CustomerArray);
y = cell(numel(f),1);
parameter = NaN(numel(f),1);
for t = 1:numel(f)
parameter(t) = s.CustomerArray.(f{t});
y{t} = zeros(100,numel(f{t}));
end
I want to pass a particular column of the matrix that I have in my program to a function.
If I use the call <function_name>(<matrix_name>[][<index>]); as the call
then I get the error
error: expected expression before ‘]’
token
So please help me in finding the suitable way
Thanks
The syntax you used doesn't exist.
Matrices are stored in memory by row (or better, by the second dimension, to which you give the semantics of rows), so you cannot natively. You could copy all your column elements in a vector (a single dimension array) and pass it.
If you need to work only by column (and never by row) you could change the semantics you give to the first and to the second dimension: think of your matrix as matrix[row][column] instead of matrix[column][row].
Otherwise, if you need to do this often, look for a better data structure, instead of a simple array.
Because of the way addressing works, you can't simply pass the 'column' since, the 'column' values are actually stored in your 'rows'. This is why your compiler will not allow you to pass no value in your 'row' reference, ie: '[]'.
An easy solution would be to pass the entire matrix, and pass the column index as a seperate integer, and the number of rows. Your function can then iterate through every row to access all members of that column.
ie:
functionName(matrixType** matrixRef, int colIndex, int numRows)
{
for(int i=0; i< numRows; ++i)
matrixType value = matrixRef[i][colIndex]; //Do something
}
You are going to have to reformat the data. The column is not contiguous in memory. If for example you have an array:
arr[5][4]
Then trying to pass a 'column' would be like trying to pass every 5th element in the array. Think of it as one giant array.
There's a few things you need to keep in mind about C here.
I'm assuming your matrix is stored as a 2D array, like this:
float mat[4][4];
What you need to remember is that this is just 16 floats stored in memory consecutively; the fact that you can access mat[3][2] is just a shortcut that the compiler gives you. Unfortunately, it doesn't actually pass any of that metadata on to other function calls. Accessing mat[3][2] is actually a shortcut for:
mat[ (3*4 + 2) ]
When you pass this into a function, you need to specify the bounds of the matrix you're passing in, and then the column number:
void do_processing(float* mat, int columns, int rows, int column_idx)
Inside this function, you'll have to calculate the specific entries yourself, using the formula:
mat[ (column_idx * rows) + row_idx ]
I have an array of 20 items long and I would like to make them an output so I can input it into another program.
pos = [0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,]
I would like to use this as inputs for another program
function [lowest1, lowest2, highest1, highest2, pos(1), pos(2),... pos(20)]
I tried this and it does not work is there another way to do this?
I'm a little confused why you'd want to do that. Why would you want 20 outputs when you could just return pos as a single output containing 20 elements?
However, that said, you can use the specially named variable varargout as the last output variable, and assign a cell to it, and the elements of the cell will be expanded into outputs of the function. Here's an example:
function [lowest1, lowest2, highest1, highest2, varargout] = myfun
% First set lowest1, lowest2, highest1, highest2, and pos here, then:
varargout = num2cell(pos);
If what you're trying to do is re-arrange your array to pass it to another Matlab function, here it is.
As one variable:
s=unique(pos);
q=[s(1) s(2) s(end-1) s(end) pos];
otherFunction(q);
As 24 variables:
s=unique(pos); otherFunction(s(1), s(2), s(end-1), s(end), pos(1), pos(2), pos(3), pos(4), pos(5), pos(6), pos(7), pos(8), pos(9), pos(10), pos(11), pos(12), pos(13), pos(14), pos(15), pos(16), pos(17), pos(18), pos(19), pos(20));
I strongly recommend the first alternative.
Here are two examples of how to work with this single variable. You can still access all of its parts.
Example 1: Take the mean of all of its parts.
function otherFunction(varargin)
myVar=cell2mat(varargin);
mean(myVar)
end
Example 2: Separate the variable into its component parts. In our case creates 24 variables named 'var1' to 'var24' in your workspace.
function otherFunction(varargin)
for i=1:nargin,
assignin('base',['var' num2str(i)],varargin{i});
end
end
Hope this helps.
Consider using a structure in order to return that many values from a function. Carefully chosen field names make the "return value" self declarative.
function s = sab(a,b)
s.a = a;
s.b = b;