Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Say we have the following matrix:
2 5
5 3
6 3
6 4
What I'm trying to do is:
1- Find the maximum value of each row
For this part, I think we can do the following?
[r,c] = size(u);
for i=1:c
for j=1:r
index=1;
for i=1:c
for j=1:r
[value,position]=max(u(j,:));
membershipMatrix(index)=value;
index=index+1;
end
end
2- Then, I want to reshape the above matrix to a 2x2 matrix.
I think we can here do the following?
reshape(I,2,2)
At the beginning it sounds trivial. I tried performing step 1 above on a 65536x2 matrix, but ended up with a 131072x1matrix, where I was originally planning to reshape into a 256x256 matrix, as I thought that I will end up with a 65536x1 matrix for the first step.
What could be going wrong?
Thanks.
When in Matlab - vectorize!
mx = max( u, [], 2 ); % find max along rows of u
reshape( mx, 256, 256 );
Related
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 2 years ago.
Improve this question
I loaded in the 1000x1 .txt file but I need to make it 100x10? How do I do this in matlab?
Thanks
Reshape should do this for you. https://www.mathworks.com/help/matlab/ref/reshape.html
But you haven't explained any criteria regarding what the change of dimensions is based on. So, it may not be exactly what you want.
You can use the reshape function to do this. In the example below I created a column vector x that is 1000 x 1 containing numbers that ramp from 1 to 1000. I didn't know what order you wanted the rows and columns populated, so I created variables x2 and x3 with the two variations, you can choose the form that fits your needs.
x = (1:1000)';
% x2 is created with one column at a time
x2 = reshape(x, 100, 10);
% x3 is created one row at a time
x3 = reshape(x, 10, 100)';
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 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 6 years ago.
Improve this question
I need to start with the highest value in the matrix (i,j). Then, go backwards to one of positions (i-1,j), (i, j-1), and (i-1, j-1) depending on the direction of movement used to construct the matrix. This method is used throughout until a matrix cell with zero value is reached.The above image shows what I want, I need to trace those marked in blue. I know there is a max function in matlab.
If I understand what you try to do...
Suppose M is your matrix:
result=[];
[~,ind]=max(M(:));
[r,c]=ind2sub(size(M),ind);
result=[result; r c];
while M(r,c)~=0
[~,f]=max([M(r-1,c),M(r,c-1),M(r-1,c-1)]);
switch f
case 1
r=r-1;
case 2
c=c-1;
case 3
r=r-1;
c=c-1;
end
result=[result; r c];
end
Then, result is a 2 x n matrix, where each row is the indexes of one step- from the upper to lower.
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