Adding value to a cell element specified by array - matlab

I am looking for a vectorized solution!
I have two arrays for example:
idx=[1 1 1 2 2 3 3 4 4 4 5 5 6 7 8 8 8 9 9]; %%//Integers,sorted
val=[1 4 8 2 5 3 9 1 4 8 2 5 6 7 1 4 8 3 9]; %%//respective Values (could be anything)
Now i want to create a cell array which contains in his elements specified by idx the according values of val. So the result should be a [9x1] cell with:
[1 4 8]
[2 5]
[3 9]
[1 4 8]
[2 5]
[6]
[7]
[1 4 8]
[3 9]
I know I could loop over the values from 1 to 9 and use horzcat while idx is equal to my loop index but I am looking for a vectorized solution. Reason is, that I am trying to change a loop solution of a problem to vectorized solution yet I am stuck here

Use accumarray:
out = accumarray(idx(:),val(:),[],#(x){x},{});

mat2cell(val,1,diff([0,find(diff(idx)),numel(idx)]))
Maybe someone finds a possibility to get rid of the find, then it's probably faster.

Related

Combine columns of a matrix to vectors

How can I combine columns of a matrix to vectors?
I have:
1 2 3
4 5 6
7 8 9
and I want to have a struct with the field 'positions' that contains a vector in each row like this:
[1 2 3]
[4 5 6]
[7 8 9]
is this what you need?
A=reshape(1:9,[3 3])';
st=cell2struct(mat2cell(A,ones([1 size(A,1)]),size(A,2)),'positions',size(A,2))
with that you get struct array.
>> st(1).positions
ans =
1 2 3

How can I plot part of a simple vector?

Suppose I've got the following vector and plot it from index 1 to 11:
a = [1 2 5 4 5 4 2 3 7 1 5];
plot(a);
How can I plot only part of this vector? like from index 3 to index 7. I found this question but couldn't figure out how to use it.
a = [1 2 5 4 5 4 2 3 7 1 5];
plot(a(3:7));

Subtraction by tracking x y coordinates

I have set of arrays:
x1=[1 2 3 4 5 6 7 8 9];
y1=[1 2 3 4 5 6 7 8 9];
z1=[2 2 2 2 2 9 6 2 2];
and
x2=[6 7];
y2=[6 7];
z2=[2 2];
by tracking x y coordinates, the z arrays have to be subtracted so that output will be
x=[1 2 3 4 5 6 7 8 9];
y=[1 2 3 4 5 6 7 8 9];
z=[2 2 2 2 2 7 4 2 2];
You can get the indicies of your elements in z using the ismember function:
a1=[x1.',y1.'];
a2=[x2.',y2.'];
[~,ix]=ismember(a2,a1,'rows')
z1(ix)=z1(ix)-z2
To use the ismember two matrices a1 and a2 are created witch contain the coordinates in rows. Then ismember with 'rows' option is used to get the indices.
When a point exists in x2/y2 which does not exist in x1/y1 the above code will fail.

How to convert an Image matrix into a vector in Matlab

I cant seem to figure this out:
I need to reshape a matrix into a vector and so far I have this:
img = imread('image.png');
grayImage = rgb2gray(img);
imageArray = reshape(grayImage, r, c);
Which outputs something like:
imgVector=[1 2 3 4 5 6 7 8 9 0]
My problem is I need it to do something like this:
imgArray=[1 2 3
4 5 6
7 8 9]
Reshaped into:
imgVector=[1 2 3 6 5 4 7 8 9]
I hope that make sense. Basically I need it to be unzipped so it goes from left to right and then right to left after the next row. Any help would be appreciated. Thank you in advance.
Fundamentally what you're trying to do is flip each row left-to-right, so the built-in function fliplr will do the trick.
To do it in a single step, just select every other row in your indexing operation:
>> imgArray=[1 2 3; 4 5 6; 7 8 9]
imgArray =
1 2 3
4 5 6
7 8 9
>> imgArray(2:2:end,:)=fliplr(imgArray(2:2:end,:))
imgArray =
1 2 3
6 5 4
7 8 9
Then you can turn it into a vector by reshaping.
imgVector=reshape(imgArray',1,[]);
#%transpose the array----^
Since reshaping is done column-wise, transpose the array first to get it in the format you want.
You can use the fliplr function, which inverts the order of a vector.
Here is a simple example:
A = [1 2 3;4 5 6;7 8 9];
A =
1 2 3
4 5 6
7 8 9
A(2,:) = fliplr(A(2,:));
A =
1 2 3
6 5 4
7 8 9
So could use a loop an flip every other row for your entire image. Hope that helps!

Finding indices of multiple items in a matrix?

I have an array of elements and I want to see the indices of them inside another matrix
For example, for
A = [1 2 3]
B = [1 2 3 4 5 3 4 5 1 2 3]
then result array
C = [1 2 3 6 9 10 11]
that give the indices of 1 2 3
Is there any function or a short way to handle?
I think you want:
find(ismember(B, A))
ans =
1 2 3 6 9 10 11