Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to generate an array of random numbers that are divisible by 3 in matlab. What is the way to do this?
try this:
a = randi(100, 5) * 3;
a = a(:);
use randi(100, 5) to create a pseudo random 5x5 matrix of integers between 1 and 100. multiply by 3 to make them all divisible by 3. use the colon operator : to convert it to a 25 long array.
As suggested by #tashuhka you can avoid the : by:
a = randi(100, [25,1]) * 3;
You can read all about it here
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
In a sample code I have X(1:n, 1)
I don't understand how it works
n=3 in a sample
Is it a matrix? but isn't () used for indexing so how it's a double index?
I imagine that your X is a two-dimmensional matrix. Generalizing, it can be said that to access the elements of a matrix is done with: X(n,m).
The common case is to get one single number, in that case n and m are integer numbers. But you can also pass vectors to n and m positions and that way extract a submatrix from the original one.
As an example:
X = [1 2 3; 4 5 6; 7 8 9]
X(1:3,1) = [1; 4; 7]
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I understand how the program works but I have a little bit of confusion. If anybody can explain, that will be great. The output is 21, 12. Does it work like 7*3=21 and 4*3=12?
mat=[7 11 3; 3:5];
[r,c]=size(mat);
for i=1:r
fprintf ('The sum is %d\n',sum(mat(i,:)))
end
mat(i,:) will give you all values in the first row of mat. In your example, this first row is [7 11 3], and the second row is [3 4 5]. The outputs you're seeing are the sums of all values in each row (7+11+3=21).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I would like to pick two different random elements from given array with their positions. Similar like with datasample, but with datasample there is a possibility of picking the same element twice.
I could use while loop or similar, but I suppose there is an easier way to do it.
Say you have a matrix A:n by m, you can choose two elements at random as following,
A=[2 7 8;5 4 6;8 3 11];%given array
[n m]=size(A);
x=2;%two different random elements
i=randperm(n,x)%row index for x elements
j=randperm(m,x)%column index for x elements
A(i(1),j(1)) %First random element
A(i(2),j(2)) %Second random element
If you try this you could get something like,
i =
2 3
j =
2 1
ans =
4
ans =
8
the code can be further simplified but just wanted to make it clear. Please let me know if you have any additional questions or require further clarification.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I need to do an optimization problem using ga algorithm. The number of decision variable is 3. i.e 3 x 1 matrix. but first element is always 0. keeping this as reference I want to optmize other two variables. How can I do this task?
Then just use
lb = [0,0,0];
ub = [0,8,8];
IntCon = [0 2 3];
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have the following data: ET = [1 3 5 7 6 4], and below is my code:
for i=1:3
meanET(i)=ET(:,1+(2*i-2)); %//for i=1,extract ET column 1 data
stdET(i)=ET(:,2+(2*i-2));
totalET(i)=meanET(i)+stdET(i)
end
However, MATLAB display's an error that says that in the assignment A(I)=B, the number of elements in B and I must be the same, and therefore I modified my code to this:
for i=1:3
meanET=ET(:,1+(2*i-2));%for i=1,extract ET column 1 data
stdET=ET(:,2+(2*i-2));
totalET=meanET+stdET
end
After running the latter code, it showed meanET=6, stdET=4, and totalET=10, which means that it only stored the data for i=3 in the workspace. I want to get the result like
totalET=[4 12 10] in the workspace, corresponding to i = 1, 2, 3. How do I do that?
OR you could just go with a simple vectorized solution:
>> totalET = ET(1:2:5) + ET(2:2:6)
totalET =
4 12 10
you should just declare your target array at the beginning of your code:
meanET=zeros(size(ET,1),3);
stdET=zeros(size(ET,1),3);
for i=1:3
meanET(:,i)=ET(:,1+(2*i-2));
stdET(:,i)=ET(:,2+(2*i-2));
end
totalET=meanET+stdET