I would like to create something to store string, for example:
for x = 1:3
fruit = strcat('orange', num2str(x));
A = {fruit};
how can I make an output of a 1x3 matrix of
A =
orange1
orange2
orange3
I have tried a few things but nothing worked.
I do not think it is complicated, but I just don't seem to get my head round it.
and after I completed this, would I be able to combine a normal numerical matrix with A such that:
N = [1 2; 3 4; 5 6];
FINAL = [N A];
>>output of FINAL would look like
FINAL =
1 2 orange1
3 4 orange2
5 6 orange3
In MatLab, numerical arrays can only be concatenated with numerical arrays. If you want to create an array with varying data types, you need to use Cell Arrays.
To answer your first question, I would advise you to first declare fruit as a cell array, and then fill it with the desired data :
fruit = cell(3,1);
for i =1:3
fruit{i} = strcat('orange',num2str(i));
end
fruit
This should produce the desired output.
For your second question, if you want to concatenate a numerical array with a cell array, you first need to convert it to a cell array using num2cell, such as :
N = [1 2;3 4;5 6];
FINAL = [num2cell(N),fruit]
In that case, FINAL will be an array of 9 cells, that you could access like FINAL{1,3} = orange1. To write compact code with cells, you should take a look at cellfun and deal which are two useful functions.
Hope this helps !
for x = 1:3
fruit = ['orange', num2str(x)];
A{x,1} = fruit;
end
N = [1 2; 3 4; 5 6];
N_as_cell = num2cell(N);
FINAL = cat(2, N_as_cell, A);
Related
This question already has answers here:
How to get the cartesian product of multiple lists
(17 answers)
Closed 2 years ago.
I have the following two lists:
x = [1,2]
y = [4,5,6]
I want to iterate x by z.
I have a variable called code set to NONE and another variable called value also set to NONE. Here is the output I am aiming for:
1st iteration, code = 1 and value = 4
2nd iteration, code = 1 and value = 5
3rd iteration, code = 1 and value = 6
4th iteration, code = 2 and value = 4
5th iteration, code = 2 and value = 5
6th iteration, code = 2 and value = 6
Here is what I have tried:
x = [1, 2]
y = [4, 5, 6]
code = None
value = None
for x_ids, y_ids in zip(x, y):
code = x_ids
value = y_ids
print("c", code)
print("v", value)
output:
c 1
v 4
c 2
v 5
Can anyone suggest how to get the output described above?
This is one way to achieve what you're looking for:
x = [1, 2]
y = [4, 5, 6]
code = None
value = None
iter_count = 0
for x_ids in x:
code = x_ids
for y_ids in y:
iter_count += 1
value = y_ids
print('{} iteration, code = {} and value = {}'.format(iter_count, code, value))
#print(str(iter_count) + ' iteration, code = ' + str(code) + 'and value = ' + str(value))
Like discussed in the comments, this code iterates through all elements of y for every element in x. In your original code, you were iterating through both lists all at ones, using zip. Since you want to print the number of iteration too, there is a new variable, iter_count, that counts and stores those.
The code has two print statements, they print the same messages. The commented out one concatenates strings, and numbers converted to strings. The uncommented one may be less intuitive but it is often more useful and cleaner. It's worth looking at it, you can find an introduction here.
Last thing, if you need that too - to print numbers in 1st, 2nd etc. format you can use some of these approaches.
I'm trying to use a cell array of strings in order to build a legend for each sample in a graph, something like this:
x = [1 5 3 5 6];
y = [3 6 3 6 4];
Legend = cell(5,1);
Legend{1} = 'a';
Legend{2} = 'b';
Legend{3} = 'c';
Legend{4} = 'd';
Legend{5} = 'e';
figure
hold on
for k=1:5
plot(x(k),y(k),'o')
end
legend(Legend)
But I'm getting the following message error from matlab:
Error using subsindex
Function 'subsindex' is not defined for values of class 'cell'.
Is there something that I can do to fix it?
Thanks
I'm using MATLAB R2009a and following this example:
http://uk.mathworks.com/help/matlab/matlab_external/using-a-matlab-application-as-an-automation-client.html
I'd like to edit it so that I can write a matrix of unknown size into a column in an excel sheet, therefore not explicitly stating the range. I've attempted it this way:
%Put MATLAB data into the worksheet
Hop = [47; 53; 93; 10]; %Pretend I don't know what size this matrix is.
p = length(Hop);
p = strcat('A',num2str(p));
eActivesheetRange = e.Activesheet.get('Range','A1:p');
eActivesheetRange.Value = Hop;
However, this errors out. I've tried several variations of this to no avail. For example, using 'A:B' puts this array in columns A and B in excel and a NAN into every cell beyond my array. As I only want column A filled, using simple ('Range','A') errors out also.
Thanks in advance for any advice you can offer.
You're having issues because you're trying to use your variable p in a string directly
range = 'A1:p';
'A1:p'
This isn't going to work, you want to include the value of p. There are a number of ways you can do this.
In the code you have provided, you have already set p = 'A10' so if you wanted to append that to your range, you'd perform string concatenation
p = 'A10';
range = strcat('A1:', p);
I personally prefer to use sprintf to place the number directly into my strings rather than concatenating a bunch of strings.
p = 10;
range = sprintf('A1:A%d', p)
'A1:A10`
So if we adapt your code to use this we should get
range = sprintf('A1:A%d', numel(Hop));
eActivesheetRange = e.Activesheet.get('Range', range);
eActivesheetRange.Value = Hop;
Also just to be a little explicit, I would use numel rather than length as length is ambiguous. Also, I would flatten Hop into a column vector just to make sure that it's the proper dimension to be written to the spreadsheet.
eActivesheetRange.Value = Hop(:);
Essentially, the idea is to replace xx in 'B1:Bxx' with the number of elements in your matrix.
I tried this:
e = actxserver('Excel.Application');
eWorkbook = e.Workbooks.Add;
e.Visible = 1;
eSheets = e.ActiveWorkbook.Sheets;
eSheet1 = eSheets.get('Item',1);
eSheet1.Activate;
A = [1 2 3 4];
eActivesheetRange = e.Activesheet.get('Range','A1:A4');
eActivesheetRange.Value = A;
The above is directly from the link you shared. The reason why what you are trying to do is failing is that the p you pass into e.Activesheet.get() is a variable and not a string. To avoid this, try the following:
B = randi([0 10],10,1)
eActivesheetRange = e.Activesheet.get('Range',['B1:B' num2str(numel(B))]);
eActivesheetRange.Value = B;
Here, num2str(numel(B)) will pass in a string, which is the number of elements in B. This is variable in the sense that it depends on the number of elements in B.
So I have a matrix like:
1 2 3 4
0 3 4 1
7 3 4 5
And I want to select a row,then use this row to do stuff on it such as sorting it with some algorithms;I made this so far:
def row_management(matrix):
theline = input('wich line?') #enter row number
thelist = [matrix[theline]] #go take the right row of the matrix
menu_thelist(thelist) #supossed to use the row and take it to the list management menu
However, when I run this, it always return an error "[matrix[theline]] TypeError: list indices must be integers, not str" and I don't get it.
The input call returns a str type, so it can not be used directly as index to the list inmatrix[theline], which is also what the error message says. Instead do:
matrix[int(theline)]
Needed to convert to int
theline = int(input('wich line?'))
And also
thelist = matrix[theline]
the have [1,2,3] and not [[1,2,3]](wich cause further problem)
I have two cell arrays of string A and B. All cells of B are also in A. I want to find the index of cell of B in A. Thanks.
Example:
A=
'aaaa'
'bbbb'
'cccc'
'dddd'
'ffff'
B=
'ffff'
'aaaa'
ans=
5
1
or
ans=
1
5
use either intersect or ismember
[~, idxInA] = intersect(A,B)
or
LocInA = find(ismember(A,B))
You can do this really simply, use the code below
indices = cell(size(B));
for i = 1:numel(B)
indices{i} = find(strcmpi(A,B(i)));
end
While I do recommend using ismember or intersect, those solutions will not handle case insensitive solutions. Also, those methods will not indicate how many times a specific index was matched, where my solution, will return all indices that match for each comparison.
UPDATE
Code I am running to test this.
A={'aaaa','bbbb','cccc','dddd','ffff','aaaa'};
B={'ffff','aaaa','cccc','qwerty'};
indices = cell(size(B));
for i = 1:numel(B)
indices{i} = find(strcmpi(A,B(i)));
end
indices
Which returns the following
indices =
[5] [1x2 double] [3] [1x0 double]
I do not see where you are having problems