save values of matrix in indexes of another matrix

1 次查看(过去 30 天)
hello, i want to
nimg=1;
for ii=1:imglen
for iii=1:c-4+1
hashTable(Idximgseq(ii,iii)+1,nimg)={[ii,iii]};
nimg=nimg+1;
end
end
save matrix of values in another matrix,such that each value in the first matrix saved in the same value of index in the second matix,but i have a problem that the counter increses in all times,i want to increse the counter in saparete index,such that:
first matrix(1,1)=15 saved in the index 15 colomn 1 in the second matrix,first matix(1,2)=15 saved in the index 15 colomn 2 in the second matrix,first matrix(1,3)=13 saved in the index 13 colomn 1 in the second matrix, but my problem that first matrix(1,3)=13 saved in the index 13 colomn 3 in the second matrix not in the index 13 colomn 1??

回答(1 个)

Suraj Kumar
Suraj Kumar 2024-10-4
Hi Hadeel,
To save values from one matrix into another matrix using specific indices, please follow the steps outlined below:
1. Initialize the required variables and create a ‘counters’ array to track the next available column for each index, ensuring independent management of values.
% Initialize counters for each index
counters = zeros(maxIndex, 1);
2. Iterate over the input matrix to determine indices and store pairs in hashTable. Adjust indices for 1-based indexing by incrementing the respective counter, then place the pair in the correct column.
% Populate the hashTable
for ii = 1:imglen
for iii = 1:c-4+1
index = Idximgseq(ii, iii) + 1;
counters(index) = counters(index) + 1;
hashTable{index, counters(index)} = [ii, iii];
end
end
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by