Borrar filtros
Borrar filtros

如何設定矩陣中特定元素的值

7 visualizaciones (últimos 30 días)
Dun-Ren Liu
Dun-Ren Liu el 10 de Jun. de 2022
Respondida: Gagan Agarwal el 31 de Ag. de 2023
[m1,m2]=find(www=='Ref');
f=zeros(480,640);
f(m1,m2)=1;
m1、m2分別為一個n*1的矩陣,若我想把f矩陣中每個對應的(m1,m2)元素設為1,
例如:m1=[1,2,3],m2=[4,5,6],而我想設定f(1,4)、f(2,5)及f(3,6)都等於1,
請問我該怎麼編寫代碼,謝謝。

Respuestas (1)

Gagan Agarwal
Gagan Agarwal el 31 de Ag. de 2023
Hi Dun-Ren Liu
To accomplish your desired objective, you have the option to utilize the built-in sub2ind function available in MATLAB. Please refer to the sample code provided as an example.
f = zeros(5,5);
x = [1,2,3]';
y = [1,2,3]';
idx = sub2ind(size(f),x,y);
f(idx) = 1;
In this code snippet, “f” represents the matrix whose values you intend to modify, while “x” and “y” are vectors that store the row and column indices, respectively.

Categorías

Más información sobre 矩阵和数组 en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!