This question already has answers here:
The for loop doesn't iterate
(2 answers)
Closed 9 years ago.
I was just writing a "kinda-foreach" loop in Matlab and encountered this strange behavior:
I have the matrix A:
A = [
3
9
5
0];
And I want to use a foreach loop (as explained here) on the A.
If I write this:
for i = A
disp('for')
i
end
The result will be:
for
i =
3
9
5
0
But when I use the transpose, the result will change:
for i = A'
disp('for')
i
end
Result:
for
i =
3
for
i =
9
for
i =
5
for
i =
0
Which is the result I want.
Can anybody explain what's going on here? What's the difference between these two cases?
when you type
A = [
3
9
5
0];
you create a column vector. Because Matlab iterates over columns you get one answer (the first column). By transposing it you get a row vector with 4 columns and therefore 4 answers each with one column.
In Matlab, the for loop iterates over columns. http://www.mathworks.es/es/help/matlab/ref/for.html
Related
I am trying to reverse the elements of the matrix such that for given matrix order of the elements get reversed.
my code is as shown for the 3x3 matrix is working.
X = [ 1 2 3 ; 4 5 6 ; 7 8 9 ];
B = [fliplr(X(3,:));fliplr(X(2,:));fliplr(X(1,:))];
input X =
1 2 3
4 5 6
7 8 9
output:
B =
9 8 7
6 5 4
3 2 1
the above code I am trying to generalize for any matrix with the following code
[a,b]=size(X);
for i=0:a-1
A = [fliplr(X(a-i,:))];
end
but get only last row as output.
output A =
3 2 1
please help me to concatenate all the rows of the matrix one above to each other after it got reversed.
rot90 is the function made for this purpose.
B = rot90(A,2);
Your code doesn't work because you overwrite A in every loop iteration. Instead, you should index into A to save each of your rows.
However, fliplr can flip a whole matrix. You want to flip left/right and up/down:
B = flipud(fliplr(X));
This is the same as rotating the matrix (as Sardar posted while I was writing this):
B = rot90(X,2);
A totally different approach would work for arrays of any dimensionality:
X(:) = flipud(X(:));
This question already has answers here:
How can I index a MATLAB array returned by a function without first assigning it to a local variable?
(9 answers)
Closed 5 years ago.
I'm trying to directly retrieve the second variable of a function that returns multiple variables. For example, I have the column vector a as follows:
a = [5 ; 4 ; 3 ; 2 ; 1 ; 9 ; 8 ; 7];
And I want to retrieve the index of the minimum value. I know I can do this.
[n,i] = min(a);
i
But how can I do this essentially in one line? I thought this ould work but it does not:
min(a)(1)
You can ignore one of them like this:
[n,~] = min(a);
Or:
[~,i] = min(a);
It's not pretty, but it might work for you:
find( x == min(x), 1, 'first' )
I've two matrix a and b and I'd like to combine the rows in a way that in the first row I got no duplicate value and in the second value, columns in a and b which have the same row value get the maximum value in new matrix. i.e.
a = 1 2 3
8 2 5
b = 1 2 5 7
2 4 6 1
Desired output
c = 1 2 3 5 7
8 4 5 6 1
Any help is welcomed,please.( the case for accumulation is asked here)
Accumarray accepts functions both anonymous as well as built-in functions. It uses sum function as default. But you could change this to any in-built or anonymous functions like this:
In this case you could use max function.
in = horzcat(a,b).';
[uVal,~,idx] = unique(in(:,1));
out = [uVal,accumarray(idx,in(:,2),[],#max)].'
Based upon your previous question and looking at the help file for accumarray, which has this exact example.
[ii, ~, kk] = unique([a(1,:) b(1,:)]);
result = [ ii; accumarray(kk(:), [a(2,:) b(2,:)], [], #max).'];
The only difference is the anonymous function.
This question already has answers here:
Common way to generate finite geometric series in MATLAB
(2 answers)
Closed 7 years ago.
How to calculate the first N terms of the geometric sequence Un = 2^n in Matlab?
Are there any Matlab functions that I'm not aware of to facilitate this? or do I have to pick a math book to understand this and implement it in a for loop or something?
Any links to similar Matlab code would be appreciated, or if you could explain it for me that would be appreciated!
First, you set the N terms for your sequence, i.e.:
N = 10 %//set first 10
Now you want to make a vector from 1 to N, i.e.:
n= [1:N]
Un = 2.^n %//Note the dot is very important! I almost forgot
%//ans = [2,4,8,16...1024]
This would make function a vector of 1 by N where each element is the corresponding answer to your function.
for your second question (in comment)
you want to do something like:
Bflip = B' %//This flips the matrix B so that what use to be column is now rows
So Bflip would be the result you want, I tested with your example:
A = [2 2 2;4 4 4; 6 6 6];
B = [0 0 0; 1 1 1; 2 2 2];
Bflit = [ 0 1 2
0 1 2
0 1 2]
This will generate a 3 dimension matrix. To call on each of the 4 sets of results, just do something like result1 = permutation(:,:,1)
This question already has answers here:
Get the indices of the n largest elements in a matrix
(4 answers)
Closed 8 years ago.
If I have a matrix like this:
sample = [1 0.21852382 0.090085552 0.219984954 0.446286385;
0.21852382 1 0.104580323 0.138429617 0.169216538;
0.090085552 0.104580323 1 0.237582739 0.105637177;
0.219984954 0.138429617 0.237582739 1 0.192753169;
0.446286385 0.169216538 0.105637177 0.192753169 1 ]
I want to find the top 3 max values in every rows in Matlab.
what i do in Matlab?
and is it true? i want to find top-N method in select neighbors.
I would recommend rewording your question. You say you want the top ten max values in every row, but the matrix you gave has only five columns :/
I think that what you are looking for is something like this.
sample = [1 0.21852382 0.090085552 0.219984954 0.446286385;
0.21852382 1 0.104580323 0.138429617 0.169216538;
0.090085552 0.104580323 1 0.237582739 0.105637177;
0.219984954 0.138429617 0.237582739 1 0.192753169;
0.446286385 0.169216538 0.105637177 0.192753169 1 ]
B = sort(sample,2,'descend') % will sort the rows of the array in descending order
C = B(:,1:N) % Select the top N values.
Hope this answers your question.
If that isn't what you want, try [Y,I] = max(matrix,[],desired_dimension) where Y and an array of the is the actual max values (e.g. [1 1 1 1 1]) and I is the index of the max values, (e.g [1 2 3 4 5])
EDIT
If desired_output = [1 1 1 1 1]', (a column vector, note transpose), then the command to do that is max(matrix,[],2) to operate along the second dimension. This behavior is defined in help max.