multiplying a value and create a matrix of a certain size

The outcome of this line
B=strcat(num2str(i),',',str3);
is B =4,1,1. I would like to multiply B and create a matrix (a column vector) with the same size as data1 (which is a 748*3 matrix) so that I could create a new matrix like newMatrix=[data1 B] later on . Thank you.

7 comentarios

Jan
Jan el 30 de Jun. de 2018
The output is the char vector '4,1,1'. It is not meaningful to multiply a string. You want to create a column vector, with the same size as a [748 x 3] matrix?
Please post more of you code and define the inputs accurately. It is not clear if you mean numerical arrays or char matrices.
Are you certain that it makes sense in your context to multiple a character vector by something?
Thank you for your response. Maybe better to say I want to have 4,1,1 as one unit so that I could create a vector column of 748 (number of rows of data1)"4,1,1"'s (which is B in the script).
I rewrote the code this way:
B=strcat(num2str(i),str3);
n=length(data1);
r=repmat(B,1,n)';
mergedData=[data1 SCRIPTdata r];
The problem is that B (4,1,1) was taken as 4 items and the length of the resulted vector became 4*748. I rewrote the code this time as:
B=strcat(num2str(i),str3);
n=length(data1);
r=repmat(B,1,n)';
str2double(r);
mergedData=[data1 SCRIPTdata r];
Now the length of data1, SCRIPTdata, and r is the same but the resulted mergedData contains columns of meaningless symbols. The code changes the variables, which were "double" to "char" and even the last line of the code
(str2double(r);)
did not solve the problem.
Thank you.
@Elaheh: it would help if you actually described the situation: what data you have and what you the ultimate goal is. Currently you are mixing up character arrays and numeric arrays in ways that make little sense. It would help us to take a step back, and look at the broader problem that you are solving.
Elaheh
Elaheh el 30 de Jun. de 2018
Editada: Image Analyst el 30 de Jun. de 2018
I have one numeric matrix (748*3 double) and I have the following variable:
B=strcat(num2str(i),str3);
when I type B in the command window, I come up with:
B =
41,1
and it is 1*4 char.
B is the name of the matrix (i.e., data1, 748*3) and I want to have the name with the data in the excel file, because I have about 30 persons and each person has 4 pieces of data. Writing B this way enables me to create a name for each piece of data.
How could I do that?
B is the name of a character vector and is '41,1' -- it is not a matrix, and does not have value of 'data1, 848*3'. I think your terminology is confusing everybody.
I don't know what "each piece of data" is but why do you need a new, unique name for each piece?
Please answer my questions in Walter's answer also.

Iniciar sesión para comentar.

Respuestas (1)

repmat(B, size(data1, 1), 1)
However if data1 is numeric then [data1 replicated_B] is unlikely to give you what you expect.

4 comentarios

Yes. data1 includes columns of numeric data.
What values are in str3 and i?
Any why on earth, if i is already a number, and you want to multiply numbers in B by something, and then stitch it onto the right side of data (because you said "create a new matrix like newMatrix=[data1 B]") do you want B to be a string? That makes no sense at all.
Are you sure data is a double array and not a character array? Please attach it in a .mat file.
Considering the previous information, I would like to have something like the attached file.
i is the number of each participant. It will be changed for each person. str3 shows the test version and is the name of the excel sheet the data is taken from. They are string. data1 includes 3 columns which show the reaction time, type of the stimuli etc. I need to label each piece of data (i.e., data1) for each participant and for each test version. In other words, I need B to appear with the data. It would be ideal for B, which shows the number of each participant and the test version to appear on the top of data1. I can write the information in an excel file now but do not know how to add the file ID to the data (to data1). I attached a picture to show my intended output. The picture shows the file for each person. 40 is the number of participant and 1 is the test version.
Thank you and sorry for making you confused.

Iniciar sesión para comentar.

Categorías

Preguntada:

el 30 de Jun. de 2018

Comentada:

el 30 de Jun. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by