How to get the row index with min value and specific value with one statement [closed] - matlab

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 want to get the minimum row index with specific value in a specific column.
For example,
ma = [8 1 4; 3 1 5; 1 2 4; 1 2 5]
ma =
8 1 4
3 1 5
1 2 4
1 2 5
choosing second column (col = 2) and val = 2, as you can see second column has two elements with value of 2 and I want the one with minimum index (index = 3).
So far I've come up with,
[value1,index1]=min(ma(ma(:,col) == val,1))
value1 =
1
index1 =
1

You should use,
col = 2;
val = 2;
ind = min(find(ma(:,col)==val));
which will give ind = 3.

Related

Matlab 3x3 weighted averaging filter [closed]

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
Following advise from another post, I created a basic 3x3 averaging filter as follows:
blurP = zeros(512, 512);
for i = 1:510
for j = 1:510
sum = 0;
for k = i:i+2
for l = j:j+2
sum = sum+P(k,l);
end
end
blurP(i+1,j+1) = mean2(P(i:i+2,j:j+2));
end
end
imshow(P), figure, imshow(blurP, []);
I need to create a weighted filter with two options: One counts the center element (of the 3x3 grid) twice, thus giving me ten elements in total. The other has sixteen elements in total, with the center element counted four times and the adjacent ones counted twice—only the corner elements of the 3x3 grid are counted once.
The easiest option is to just use convolution (via the conv2 function). Then designing your kernels is a simple as writing out exactly what you described:
kernel1 = [1 1 1
1 2 1
1 1 1]/10;
blurP1 = conv2(P, kernel1, 'same');
and
kernel2 = [1 2 1
2 4 2
1 2 1]/16;
blurP2 = conv2(P, kernel2, 'same');

Change of index in for loop Matlab [closed]

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 a loop like this :
for i = 1:3
M = 1
for M = M:12
while (S(M) == i)
M = M+1
end
end
end
Now for the new incremented value of M in while loop the 'for' loop is not working for this new value.
Any solutions?
The code doesn't make any sense!
You should start to use different names for your parameters,
And note that:
for index = values
program statements
:
end
Avoid assigning a value to the index variable within the body of a loop. The for statement overrides any changes made to the index within the loop.
For the second loop for M = M : 12 is the same as for M = 1 : 12.
MATLAB takes the index values at their first definition, for example,
a = [1 2];
for i = a
disp(i);
a = [1 2 3];
end
You will see that i won't accept the value 3 because at the first use of for, i is set over [1 2].

finding all factors of a number [closed]

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 am new to Matlab and for the values of a, l and w i need to find all the values for l in the data set and the corresponding w values.
a=10;
l=(0:10)
w=(0:10)
for l,d
if a == l.*w
disp(l)
disp(w)
end
end
Not sure what you want to do, but I think your code could be put as follows:
a = 10;
l = 0:a; %// actually, it would suffice to check numbers up to floor(a/2)
ind = rem(a,l)==0; %// logical index that tells if remainder is zero or not
disp([l(ind); a./l(ind)])
Result:
1 2 5 10
10 5 2 1
You could do it more directly with Matlab's factor function:
f = factor(a);
disp([f; a./f])
Result:
2 5
5 2

Reorder the numbers after delete some of them in matlab [closed]

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 a vector [1 2 3 4 ... 100] and another vector storing the numbers I want to delete like [2 3 4]. After deleting, the numbers in original vector should be mapped to another order, for example, 1->1, 5->2, 6->3, etc. Is there any efficient way to do this? Thanks a lot!
I'd use setdiff:
% original vector
A = 1:100;
% elements to be removed
B = [2 3 4 18 21];
% new order (indices)
C = randperm(numel(A)-numel(B));
% Step 1) remove the elements
[D,I] = setdiff(A,B); % ordered
D = A(I); % restore original order
% Step 2) re-order the elements
D = D(C)
You can do:
original_vector = 1:100;
delete_vector = [2 3 4];
for ii = 1:length(delete_vector)
original_vector(original_vector==delete_vector(ii)) = [];
end

why mandelbrot's boundary is 2? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I'm trying to understand why we're iterating through Mandelbrot points until |z| < 4.
why 4? is there somekind of a law? or is it based on statistical measurements?
thanks, igal
Consider the Mandelbrot set with along y=0, which would would be z(i) = z(i-1)^2 + c.
Consider when c = (x=-2, y=0)
z(0) = 0
z(1) = 0^2 + -2 = -2
z(2) = (-2)^2 + -2 = 4 - 2 = 2
z(3) = 2^2 + -2 = 4 - 2 = 2
z(...) = 2^2 + -2 = 4 - 2 = 2
This example (x=-2,y=0) is the point with the greatest magnitude that will never blow up. Thus when z^2 > 4, there is no point in further iteration since you already know it will blow up.
All other points where the magnitude of the point >= 2 will blow up.