Border check with neon - arm

With reference to my earlier question for border check condition -
Border check in image processing?
I am writing code with neon for border check.I am having below issues when writing the code :
Input :
--------------------------------
|221 220 228 223 230 233 234 235 ..
|71 73 70 78 92 130 141 143 ..
|
Requirement:
-1 * v_m1_m1 + 0 * v_m1_0 + 1 * v_m1_p1
-1 * v_0_m1 + 0 * v_0_0 + 1 * v_0_p1 --> v_out
-1 * v_p1_m1 + 0 * v_p1_0 + 1 * v_p1_p1
Pseudo Code:
for i = 0 to nrows - 1
// init row pointers
p_row_m1 = src + src_width * MAX(i-1, 0); // pointing to minus1 row
p_row_0 = src + src_width * i; // pointing to current row
p_row_p1 = src + src_width * MIN(i+1, src_width-1); // pointing to plus1 row
v_m1_m1 = vdupq_n_u32(p_row_m1[0]); // fill left vector from src[i-1][0]
v_0_m1 = vdupq_n_u32(p_row_0[0]); // fill left vector from src[i][0]
v_p1_m1 = vdupq_n_u32(p_row_p1[0]); // fill left vector from src[i+1][0]
v_m1_0 = vld1q_u32(&p_row_m1[0]); // load center vector from src[i-1][0..7]
v_0_0 = vld1q_u32(&p_row_0[0]); // load center vector from src[i][0..7]
v_p1_0 = vld1q_u32(&p_row_p1[0]); // load center vector from src[i+1][0..7]
for j = 0 to (ncols - 4) step 4 // assuming 4 elements per SIMD vector
v_m1_p1 = vld1q_u32(&p_row_m1[j+4]); // load right vector from src[i-1][0..7]
v_0_p1 = vld1q_u32(&p_row_0[j+4]); // load right vector from src[i][0..7]
v_p1_p1 = vld1q_u32(&p_row_p1[j+4]); // load right vector from src[i+1][0..7]
//
// you now have a 3x3 arrangement of vectors on which
// you can perform a neighbourhood operation and generate
// 16 output pixels for the current iteration:
//
// v_m1_m1 v_m1_0 v_m1_p1
// v_0_m1 v_0_0 v_0_p1
// v_p1_m1 v_p1_0 v_p1_p1
//
// |
// V
//
// v_out
vst1q_s32(v_out, &image_out[i][j]) // store output vector at image_out[i][j..j+15]
// shuffle vectors so that we can use them on next iteration
v_m1_m1 = v_m1_0
v_m1_0 = v_m1_p1
v_0_m1 = v_0_0
v_0_0 = v_0_p1
v_p1_m1 = v_p1_0
v_p1_0 = v_p1_p1
end_for
// for final iteration we need to handle right edge pixels...
v_m1_p1 = vdupq_n_u32(p_row_m1[ncols-1]) // fill right vector from image[i-1][ncols-1]
v_0_p1 = vdupq_n_u32(p_row_0[ncols-1]) // fill right vector from image[i][ncols-1]
v_p1_p1 = vdupq_n_u32(p_row_p1[ncols-1]) // fill right vector from image[i+1][ncols-1]
// calculate v_out as above
vst1q_s32(v_out, &image_out[i][j]) // store output vector at image_out[i][ncols_16..ncols-1]
end_for
In C code if I want to calculate for 221 (#i,j =0,0) ,With border it looks like:
221 221 220
-----------
221 |221 220
|
71 |71 73
In NEON code if I want to calculate for 221 (#i,j =0,0) ,With border it looks like:
221 221 221 221 221 220 228 223 230 233 234 235
-------------------------------
221 221 221 221 |221 220 228 223 230 233 234 235
|
71 71 71 71 |71 73 70 78 92 130 141 143
For calculating 221:
In neon code neighbours are showed below and as per requirement it is going wrong:
v_m1_m1.0 v_m1_0.0 v_m1_p1.0
v_0_m1.0 v_0_0.0 v_0_p1.0
v_p1_m1.0 v_p1_0.0 v_p1_p1.0
221 221 230
---
221 |221| 230
---
71 71 92
How Can I take care of border with generic code logic ??

It looks like you need to understand how to implement a SIMD kernel with neighbourhood operations. This is the important part:
//
// you now have a 3x3 arrangement of vectors on which
// you can perform a neighbourhood operation and generate
// 4 output pixels for the current iteration:
//
// v_m1_m1 v_m1_0 v_m1_p1
// v_0_m1 v_0_0 v_0_p1
// v_p1_m1 v_p1_0 v_p1_p1
//
// |
// V
//
// v_out
Suppose you want to implement a sum over a cross-shaped kernel, i.e.:
y[i][j] = x[i][j] + x[i-1][j] + x[i+1][j] + x[i][j-1] + x[i][j+1];
The pseudo code for this in SIMD would be:
// sum vertically: x[i][j] + x[i-1][j] + x[i+1][j]
v_out = v_m1_0;
v_out = v_out + v_0_0; // vaddq
v_out = v_out + v_p1_0; // vaddq
// add the x[i][j-1] components
v_temp = v_0_m1:v_0_0 >> 1; // vextq - use this to get a right-shifted vector
v_out = v_out + v_temp; // vaddq
// add the x[i][j+1] components
v_temp = v_0_0:v_0_p1 << 1; // vextq - use this to get a left-shifted vector
v_out = v_out + v_temp; // vaddq
At this point v_out now contains the four sums for output elements y[i][j]..y[i][j+3]. In other words we have evaluated four output points in one kernel. Now we shuffle all our vectors left by one, and load 3 new vectors for the right hand column, and do it all over again with j += 4. If you look at the original pseudo-code from the previous question you will see that the border cases are all taken care of by filling a vector with the edge value.

Related

MATLAB: Remove sub-arrays from a multidimensional array into an array of ones

I would like to construct a function
[B, ind] = extract_ones(A)
which removes some sub-arrays from a binary array A in arbitrary dimensions, such that the remaining array B is the largest possible array with only 1's, and I also would like to record in ind that where each of the 1's in B comes from.
Example 1
Assume A is a 2-D array as shown
A =
1 1 0 0 0 1
1 1 1 0 1 1
0 0 0 1 0 1
1 1 0 1 0 1
1 1 0 1 0 1
1 1 1 1 1 1
After removing A(3,:) and A(:,3:5), we have the output B
B =
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
which is the largest array with only ones by removing rows and columns of A.
As the fifteen 1's of B corresponds to
A(1,1) A(1,2) A(1,6)
A(2,1) A(2,2) A(2,6)
A(4,1) A(4,2) A(4,6)
A(5,1) A(5,2) A(5,6)
A(6,1) A(6,2) A(6,6)
respectively, or equivalently
A(1) A(7) A(31)
A(2) A(8) A(32)
A(4) A(10) A(34)
A(5) A(11) A(35)
A(6) A(12) A(36)
so, the output ind looks like (of course ind's shape does not matter):
ind = [1 2 4 5 6 7 8 10 11 12 31 32 34 35 36]
Example 2
If the input A is constructed by
A = ones(6,3,4,3);
A(2,2,2,2) = 0;
A(4,1,3,3) = 0;
A(1,1,4,2) = 0;
A(1,1,4,1) = 0;
Then, by deleting the minimum cuboids containing A(2,2,2,2), A(4,1,3,3), A(1,1,4,3) and A(1,1,4,1), i.e. after deleting these entries
A(2,:,:,:)
A(:,1,:,:)
Then the remaining array B will be composed by 1's only. And the ones in B corresponds to
A([1,3:6],2:3,1:4,1:3)
So, the output ind lists the subscripts transformed into indices, i.e.
ind = [7 9 10 11 12 13 15 16 17 18 25 27 28 29 30 31 33 34 35 36 43 45 46 47 48 49 51 52 53 54 61 63 64 65 66 67 69 70 71 72 79 81 82 83 84 85 87 88 89 90 97 99 100 101 102 103 105 106 107 108 115 117 118 119 120 121 123 124 125 126 133 135 136 137 138 139 141 142 143 144 151 153 154 155 156 157 159 160 161 162 169 171 172 173 174 175 177 178 179 180 187 189 190 191 192 193 195 196 197 198 205 207 208 209 210 211 213 214 215 216]
As the array needed to be processed as above is in 8-D, and it should be processed more than once, so can anyone give me opinions on how to composing the program doing this task fast?
My work so far [Added at 2 am (GMT-4), 2nd Aug 2017]
My idea was that I delete the sub-arrays with the largest proportion of zero one by one. And here is my work so far:
Inds = reshape(1:numel(A),size(A)); % Keep track on which 1's survive.
cont = true;
while cont
sz = size(A);
zero_percentage = 0;
Test_location = [];
% This nested for loops are for determining which sub-array of A has the
% maximum proportion of zeros.
for J = 1 : ndims(A)
for K = 1 : sz(J)
% Location is in the form of (_,_,_,...,_)
% where the J-th blank is K, the other blanks are colons.
Location = strcat('(',repmat(':,',1,(J-1)),int2str(K),repmat(',:',1,(ndims(A)-J)),')');
Test_array = eval(strcat('A',Location,';'));
N = numel(Test_array);
while numel(Test_array) ~= 1
Test_array = sum(Test_array);
end
test_zero_percentage = 1 - (Test_array/N);
if test_zero_percentage > zero_percentage
zero_percentage = test_zero_percentage;
Test_location = Location;
end
end
end
% Delete the array with maximum proportion of zeros
eval(strcat('A',Test_location,'= [];'))
eval(strcat('Inds',Test_location,'= [];'))
% Determine if there are still zeros in A. If there are, continue the while loop.
cont = A;
while numel(cont) ~= 1
cont = prod(cont);
end
cont = ~logical(cont);
end
But I encountered two problems:
1) It may be not efficient to check all arrays in all sub-dimensions one-by-one.
2) The result does not contain the most number of rectangular ones. for example, I tested my work using a 2-dimensional binary array A
A =
0 0 0 1 1 0
0 1 1 0 1 1
1 0 1 1 1 1
1 0 0 1 1 1
0 1 1 0 1 1
0 1 0 0 1 1
1 0 0 0 1 1
1 0 0 0 0 0
It should return me the result as
B =
1 1
1 1
1 1
1 1
1 1
1 1
Inds =
34 42
35 43
36 44
37 45
38 46
39 47
But, instead, the code returned me this:
B =
1 1 1
1 1 1
1 1 1
Inds =
10 34 42
13 37 45
14 38 46
*My work so far 2 [Added at 12noon (GMT-4), 2nd Aug 2017]
Here is my current amendment. This may not provide the best result.
This may give a fairly OK approximation to the problem, and this does not give empty Inds. But I am still hoping that there is a better solution.
function [B, Inds] = Finding_ones(A)
Inds = reshape(1:numel(A),size(A)); % Keep track on which 1's survive.
sz0 = size(A);
cont = true;
while cont
sz = size(A);
zero_percentage = 0;
Test_location = [];
% This nested for loops are for determining which sub-array of A has the
% maximum proportion of zeros.
for J = 1 : ndims(A)
for K = 1 : sz(J)
% Location is in the form of (_,_,_,...,_)
% where the J-th blank is K, the other blanks are colons.
Location = strcat('(',repmat(':,',1,(J-1)),int2str(K),repmat(',:',1,(ndims(A)-J)),')');
Test_array = eval(strcat('A',Location,';'));
N = numel(Test_array);
Test_array = sum(Test_array(:));
test_zero_percentage = 1 - (Test_array/N);
if test_zero_percentage > zero_percentage
eval(strcat('Testfornumel = numel(A',Location,');'))
if Testfornumel < numel(A) % Preventing the A from being empty
zero_percentage = test_zero_percentage;
Test_location = Location;
end
end
end
end
% Delete the array with maximum proportion of zeros
eval(strcat('A',Test_location,'= [];'))
eval(strcat('Inds',Test_location,'= [];'))
% Determine if there are still zeros in A. If there are, continue the while loop.
cont = A;
while numel(cont) ~= 1
cont = prod(cont);
end
cont = ~logical(cont);
end
B = A;
% command = 'i1, i2, ... ,in'
% here, n is the number of dimansion of A.
command = 'i1';
for J = 2 : length(sz0)
command = strcat(command,',i',int2str(J));
end
Inds = reshape(Inds,numel(Inds),1); %#ok<NASGU>
eval(strcat('[',command,'] = ind2sub(sz0,Inds);'))
% Reform Inds into a 2-D matrix, which each column indicate the location of
% the 1 originated from A.
Inds = squeeze(eval(strcat('[',command,']')));
Inds = reshape(Inds',length(sz0),numel(Inds)/length(sz0));
end
It seems a difficult problem to solve, since the order of deletion can change a lot in the final result. If in your first example you start with deleting all the columns that contain a 0, you don't end up with the desired result.
The code below removes the row or column with the most zeros and keeps going until it's only ones. It keeps track of the rows and columns that are deleted to find the indexes of the remaining ones.
function [B,ind] = extract_ones( A )
if ~islogical(A),A=(A==1);end
if ~any(A(:)),B=[];ind=[];return,end
B=A;cdel=[];rdel=[];
while ~all(B(:))
[I,J] = ind2sub(size(B),find(B==0));
ih=histcounts(I,[0.5:1:size(B,1)+0.5]); %zero's in rows
jh=histcounts(J,[0.5:1:size(B,2)+0.5]); %zero's in columns
if max(ih)>max(jh)
idxr=find(ih==max(ih),1,'first');
B(idxr,:)=[];
%store deletion
rdel(end+1)=idxr+sum(rdel<=idxr);
elseif max(ih)==max(jh)
idxr=find(ih==max(ih),1,'first');
idxc=find(jh==max(jh),1,'first');
B(idxr,:)=[];
B(:,idxc)=[];
%store deletions
rdel(end+1)=idxr+sum(rdel<=idxr);
cdel(end+1)=idxc+sum(cdel<=idxc);
else
idxc=find(jh==max(jh),1,'first');
B(:,idxc)=[];
%store deletions
cdel(end+1)=idxc+sum(cdel<=idxc);
end
end
A(rdel,:)=0;
A(:,cdel)=0;
ind=find(A);
Second try: Start with a seed point and try to grow the matrix in all dimensions. The result is the start and finish point in the matrix.
function [ res ] = seed_grow( A )
if ~islogical(A),A=(A==1);end
if ~any(A(:)),res={};end
go = true;
dims=size(A);
ind = cell([1 length(dims)]); %cell to store find results
seeds=A;maxmat=0;
while go %main loop to remove all posible seeds
[ind{:}]=find(seeds,1,'first');
S = [ind{:}]; %the seed
St = [ind{:}]; %the end of the seed
go2=true;
val_dims=1:length(dims);
while go2 %loop to grow each dimension
D=1;
while D<=length(val_dims) %add one to each dimension
St(val_dims(D))=St(val_dims(D))+1;
I={};
for ct = 1:length(S),I{ct}=S(ct):St(ct);end %generate indices
if St(val_dims(D))>dims(val_dims(D))
res=false;%outside matrix
else
res=A(I{:});
end
if ~all(res(:)) %invalid addition to dimension
St(val_dims(D))=St(val_dims(D))-1; %undo
val_dims(D)=[]; D=D-1; %do not try again
if isempty(val_dims),go2=false;end %end of growth
end
D=D+1;
end
end
%evaluate the result
mat = prod((St+1)-S); %size of matrix
if mat>maxmat
res={S,St};
maxmat=mat;
end
%tried to expand, now remove seed option
for ct = 1:length(S),I{ct}=S(ct):St(ct);end %generate indices
seeds(I{:})=0;
if ~any(seeds),go=0;end
end
end
I tested it using your matrix:
A = [0 0 0 1 1 0
0 1 1 0 1 1
1 0 1 1 1 1
1 0 0 1 1 1
0 1 1 0 1 1
0 1 0 0 1 1
1 0 0 0 1 1
1 0 0 0 0 0];
[ res ] = seed_grow( A );
for ct = 1:length(res),I{ct}=res{1}(ct):res{2}(ct);end %generate indices
B=A(I{:});
idx = reshape(1:numel(A),size(A));
idx = idx(I{:});
And got the desired result:
B =
1 1
1 1
1 1
1 1
1 1
1 1
idx =
34 42
35 43
36 44
37 45
38 46
39 47

Vectorize 2d convolution on matlab

I got this Code for computing two dimensional convolution for two given arrays.
[r,c] = size(x);
[m,n] = size(y);
h = rot90(y, 2);
center = floor((size(h)+1)/2);
Rep = zeros(r + m*2-2, c + n*2-2);
return
for x1 = m : m+r-1
for y1 = n : n+r-1
Rep(x1,y1) = x(x1-m+1, y1-n+1);
end
end
B = zeros(r+m-1,n+c-1);
for x1 = 1 : r+m-1
for y1 = 1 : n+c-1
for i = 1 : m
for j = 1 : n
B(x1, y1) = B(x1, y1) + (Rep(x1+i-1, y1+j-1) * h(i, j));
end
end
end
end
How can i vectorize it , so no for loops exist ?
Thanks in advance.
Here's what I came up with:
%// generate test matrices
x = randi(12, 4, 5)
y = [2 2 2;
2 0 2;
2 2 2]
[r,c] = size(x);
%[m,n] = size(y); %// didn't use this
h = rot90(y, 2);
center = floor((size(h)+1)/2);
Rep = zeros(size(x)+size(h)-1); %// create image of zeros big enough to pad x
Rep(center(1):center(1)+r-1, center(2):center(2)+c-1) = x; %// and copy x into the middle
%// all of this can be compressed onto one line, if desired
%// I'm just breaking it out into steps for clarity
CRep = im2col(Rep, size(h), 'sliding'); %// 'sliding' is the default, but just to be explicit
k = h(:); %// turn h into a column vector
BRow = bsxfun(#times, CRep, k); %// multiply k times each column of CRep
B = reshape(sum(BRow), r, c) %// take the sum of each column and reshape to match x
T = conv2(Rep, h, 'valid') %// take the convolution using conv2 to check
assert(isequal(B, T), 'Result did not match conv2.');
Here are the results of a sample run:
x =
11 12 11 2 8
5 9 2 3 2
7 9 3 4 8
7 10 8 5 4
y =
2 2 2
2 0 2
2 2 2
B =
52 76 56 52 14
96 120 106 80 50
80 102 100 70 36
52 68 62 54 34
T =
52 76 56 52 14
96 120 106 80 50
80 102 100 70 36
52 68 62 54 34

Matlab: Creating a binned RGB histogram [duplicate]

This question already has an answer here:
Content-Based Image Retrieval and Precision-Recall graphs using Color Histograms in MATLAB
(1 answer)
Closed 7 years ago.
I want to implement the following Matlab function:
function hist = binnedRgbHist(im, numChannelBins)
Given an image im and a number between 1 and 256 numChannelBins, it should create a histogram sized (numChannelBins)^3.
For example, if numChannelBins is 2, it should produce the following 8-sized histogram:
Number of pixels with R < 128, G < 128, B < 128
Number of pixels with R < 128, G < 128, B >= 128
Number of pixels with R < 128, G >= 128, B < 128
Number of pixels with R < 128, G >= 128, B >= 128
Number of pixels with R > 128, G < 128, B < 128
Number of pixels with R > 128, G < 128, B >= 128
Number of pixels with R > 128, G >= 128, B < 128
Number of pixels with R > 128, G >= 128, B >= 128
It is like creating a cube where each axis represents one of (R,G and B), where each axis is divided into 2 bins => Finally there are 8 bins in the cube.
My questions:
It there a built-in function for it?
If not, how is it better to implement it in manners of runtinme using the GPU? Should I better iterate over the pixels once and create the histogram manually, or should I better iterate over the bins and each time count the number of pixels which satisfy the bin's conditions?
accumarray is very suited for this. Let
im: input image;
N: number of bins per color component.
Then
result = accumarray(reshape(permute(ceil(im/255*N), [3 1 2]), 3, []).', 1, [N N N]);
How it works
ceil(im/255*N) quantizes each color vaue to 1, 2, ..., N.
reshape(permute(..., [3 1 2]), 3, []).' transforms the quantized image into a three-column matrix where each row is a pixel and each column is a (quantized) color component.
accumarray(..., 1, [N N N]) considers each row of that matrix as 3D index, and counts how many times each index appears, giving filling indices that don't appear with a 0.
Example 1
Data:
>> N = 2;
>> im = randi(256,4,5,3)
im(:,:,1) =
113 152 157 65 229
138 71 215 39 41
13 108 230 160 153
142 128 125 220 214
im(:,:,2) =
208 215 182 27 230
205 161 8 95 180
225 53 73 129 31
103 97 160 83 255
im(:,:,3) =
242 29 185 89 55
202 225 156 174 96
160 197 35 87 113
244 176 146 85 120
Result:
result(:,:,1) =
1 1
3 4
result(:,:,2) =
2 4
3 2
It can be checked for example that there is only 1 pixel with all R,G,B less than 128.
Example 2
Data:
>> im = repmat(150,20,30,3);
>> N = 4;
Result:
result(:,:,1) =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
result(:,:,2) =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
result(:,:,3) =
0 0 0 0
0 0 0 0
0 0 600 0
0 0 0 0
result(:,:,4) =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
In this case all pixels belong to the same 3D-bin:
I see #Luis Mendo gave a great one-liner solution as I was writing this. In case it provides some deeper intuition, my solution makes use of histcounts and accumarray:
im = randi([1 255],[10,5,3]); %// A random 10-by-5 "image"
numChannelBins = 2;
[~,~,binR]=histcounts(im(:,:,1),[1 ceil((1:numChannelBins)*(255/numChannelBins))]);
[~,~,binG]=histcounts(im(:,:,2),[1 ceil((1:numChannelBins)*(255/numChannelBins))]);
[~,~,binB]=histcounts(im(:,:,3),[1 ceil((1:numChannelBins)*(255/numChannelBins))]);
hist=accumarray([binR(:) binG(:) binB(:)],1,[numChannelBins,numChannelBins,numChannelBins])
Explanation:
the three calls to histcounts bin the red, green, blue pixels separately -- the third output [~,~,binX] of histcounts gives the bin index for each pixel
accumarray accumulates all the unique index triplets

Find median position points of duration events

I have the following vector A:
A = [34 35 36 5 6 7 78 79 7 9 10 80 81 82 84 85 86 102 3 4 6 103 104 105 106 8 11 107 201 12 202 203 204];
For n = 2, I counted the elements larger or equal to 15 within A:
D = cellfun(#numel, regexp(char((A>=15)+'0'), [repmat('0',1,n) '+'], 'split'));
The above expression gives the following output as duration values:
D = [3 2 7 4 6] = [A(1:3) **stop** A(7:8) **stop** A(12:18) **stop** A(22:25) **stop** A(28:33)];
The above algorithm computes the duration values by counting the elements larger or equal to 15. The counting also allows less than 2 consecutive elements smaller than 15 (n = 2). The counter stops when there are 2 or more consecutive elements smaller than 15 and starts over at the next substring within A.
Eventually, I want a way to find the median position points of the duration events A(1:3), A(7:8), A(12:18), A(22:25) and A(28:33), which are correctly computed. The result should look like this:
a1 = round(median(A(1:3))) = 2;
a2 = round(median(A(7:8))) = 8;
a3 = round(median(A(12:18))) = 15;
a4 = round(median(A(22:25))) = 24;
a5 = round(median(A(28:33))) = 31;
I edited the question to make it more clear, because the solution that was provided here assigns the last number within the row of 2 or more consecutive numbers smaller than 15 (3 in this case) after A(1:3) to the next substring A(7:8)and the same with the other substrings, therefore generating wrong duration values and in consequence wrong median position points of the duration events when n = 2 or for any given even n.
Anyone has any idea how to achieve this?

Finding location of elements w.r.t original matrix after reshape in Matlab

I have a 1*262144 matrix which I have reshaped into a 512*512 matrix. Now, I need certain elements from my 2nd matrix and want to know their location as it was in the original row matrix. Say, i need element which is at (256,4) in my reshaped matrix. How can I know the position of this element in my original row only matrix?
matri_working_now = C(1,:);
matrix_working_now = reshape(matri_working_now,512,512);
[nrows,ncols] = size(matrix_stables); %matrix_stables is a matrix over which I am looping over which contains the locations of the desired elements as per the reshaped matrix. this itself is a 30839*2 matrix
for row = 1:nrows
for col = 1:ncols
%sub2ind(size(matrix_working_now),row,col)
%fprintf('iteration is equal to %6.2f.\n',row,col);
[rowss colum] = ind2sub(size(matri_working_now),sub2ind(size(matrix_working_now),matrix_stable(row),matrix_stable(col))); % i am accessing the elements of matrix_stables which provide me the row and column numbers;
end
end
Any suggestions/ideas?
Thanks!
Since your original matrix is a vector, you only need to convert from subindices to linear index, with sub2ind:
col = sub2ind(size(reshapedMatrix), 256,4);
In general, if the original matrix is not necessarily a vector, you need a second step with ind2sub:
[row col] = ind2sub(size(originalMatrix), sub2ind(size(reshapedMatrix), 256,4));
Example:
>> originalMatrix = (1:10).^2
originalMatrix =
1 4 9 16 25 36 49 64 81 100
>> reshapedMatrix = reshape(originalMatrix, 2,5)
reshapedMatrix =
1 9 25 49 81
4 16 36 64 100
>> reshapedMatrix(2,3)
ans =
36
>> [row col] = ind2sub(size(originalMatrix), sub2ind(size(reshapedMatrix), 2,3))
row =
1
col =
6
>> originalMatrix(row,col)
ans =
36

Resources