Problem with answer in my code
5 views (last 30 days)
Show older comments
I have this code below, and it should give answer like this at the top of the screenshot, but it giving this another answer, idk what problem. I need code where i will get 5 vectors(cause i have 5 index in matrix) and every vector should have one max value, for example if we will take first vector it will have the max number from first index and the min number in each subsequent index, second vector will have max number from second index and min number in all others vectors.
Hope you will understand
Hope you will understandX = [2 5; 3 6; 1 7; 3 6; 1 5];
[m, n] = size(X);
for i = 1:n
temp = X(:, i);
for j = 1:m
if X(j, i) == max(temp)
temp(j) = min(temp);
elseif X(j, i) == min(temp)
temp(j) = max(temp);
end
end
eval(sprintf('X%d = temp'';', i));
end
% show results
for i = 1:n
eval(sprintf('disp([''X%d = '', mat2str(X%d)]);', i, i));
end
Answers (1)
Elijah Gendron
on 16 Mar 2023
Would be easier to do something along these lines:
This will give you the matrix that you can then strip into indvidual rows if you want
XA = [2 3 1 3 1]; % Set the variables that will make the 'base' matrix
XB = [5 6 7 6 5]; % Set the diagonal values
n = length(XA);
A = repmat(XA,n,1); % Generate the base matrix
B = XB.*eye(n); % Generate diagonals
ix = find(B); % Get indicies of non zero values
A(ix) = B(ix); % Replace diagonal values in A matrix
disp(A)
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!