Problem with answer in my code
5 ビュー (過去 30 日間)
古いコメントを表示
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
回答 (1 件)
Elijah Gendron
2023 年 3 月 16 日
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!